How to disable a submit button after submission in PHP?

前端 未结 8 2210
耶瑟儿~
耶瑟儿~ 2021-01-27 08:21

I browsed through the other questions on this issue but couldn\'t find a satisfactory answer. I have a quiz website where the user selects an option from four options and then c

8条回答
  •  野性不改
    2021-01-27 08:43

    Common practice these days would be to use AJAX but this doesn't answer the question, nor would any Javascript solution without any assistance from the back-end. Say you have a form, for example:

    When submitting to your PHP file for processing you will be accessing your form data either via $_GET or $_POST and doing whatever you need to do with it. At this point you will be able to deduce whether or not the form has been submitted or is successful - based on this you can simply flag either input or button elements with the disabled attribute, here's a basic example:

    
    

    This would then output your new form with the button disabled based on your conditions. I should also note that depending on your doctype you may need to give the attribute a value for it to be considered valid markup, but generally speaking:

    
    
    
    
    
    

    Some related StackOverflow reading:

    What is a doctype?
    What does ?: mean? (ternary operator)
    PHP: How can I submit a form via AJAX (jQuery version)

提交回复
热议问题