How to disable a submit button after submission in PHP?

前端 未结 8 2075
耶瑟儿~
耶瑟儿~ 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:27

    For disable write like this, after once click it will be disable.

    <input type="submit" id="but_sub" name="submit" \>
    
    
        $("#but_sub").click(function() {
            $(this).attr("disabled",true);
        }
    

    or

        $('#but_sub').click(function() {
            $(this).attr("disabled","disabled");
        }
    
    0 讨论(0)
  • 2021-01-27 08:31

    Try this. You might want to pass the value of each answer to a PHP file that performs backend work via AJAX.

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
    <script>
        $('#question').submit(function(){
           //Get the value of the answer that was checked
           var answer = $('input:radio[name=question_one]:checked').val();
    
           //Use jQuery's AJAX function to send the data to a PHP file that does backend work 
           $.ajax({  
              type:'POST',  
              url:'quiz_backend.php',  
              data:'answer='+ answer,
              success: function() {
                  //Hide the submit button upon successful submission
                  $('#submit').attr("disabled", true);
                  event.preventDefault();
              }
           });
        });
    </script>
    
    <form id="question">
       <input type="radio" name="question_one" value="answer_one">A<br />
       <input type="radio" name="question_one" value="answer_two">B<br />
       <input type="radio" name="question_one" value="answer_three">C<br />
       <input type="radio" name="question_one" value="answer_four">D<br />
    
       <input type="submit" value="submit" id="submit"/>
    </form>
    

    More info about jQuery's AJAX call here.

    0 讨论(0)
  • 2021-01-27 08:31

    Just try with below code. I hope it will help you...

        $('#contact-form').submit(function () {
            if ($(this).valid()) {
                $('.submit').attr("disabled", true);
            }
        });
    
    0 讨论(0)
  • 2021-01-27 08:35

    Example registration button with User Message:

    <input type="submit" onclick="this.disabled=true;this.value='ADD USER MESSAGE...';this.form.submit();" class="ADD BUTTON CLASS HERE" value="Register!">
    

    .sub-btn:hover {
      color: #FFFFFF;
      background-color: #406800;
      border-color: #82B33C;
      transition: all .25s linear;
      -o-transition: all .25s linear;
      -moz-transition: all .25s linear;
      -webkit-transition: all .25s linear;
      -webkit-appearance: none;
      border-radius: 4px;
    }
    
    .sub-btn {
      border-width: 1px;
      border-style: solid;
      font-family: 'Bitter', serif;
      font-weight: 700;
      font-size: 20px;
      margin-left: 0;
      padding: 10px;
      width: 100%;
      color: #f8f8f8;
      background-color: #82B33C;
      border-color: #406800;
      transition: all .25s linear;
      -o-transition: all .25s linear;
      -moz-transition: all .25s linear;
      -webkit-transition: all .25s linear;
      -webkit-appearance: none;
      border-radius: 4px;
    }
    
    .tx-shadow {
      text-shadow: 0px 2px 2px rgba(0, 0, 0, 0.45);
    }
    <input type="submit" onclick="this.disabled=true;this.value='Creating Account, please wait...';this.form.submit();" class="sub-btn tx-shadow" value="Register!">

    0 讨论(0)
  • 2021-01-27 08:40

    very easy javascript only method

    <form action="" method="post" >
    <input type="submit" name="SUBMIT" value="Submit Form"
       onclick="this.disabled='disabled';this.form.submit();" />
    </form>
    
    0 讨论(0)
  • 2021-01-27 08:41

    I leave a code:

        <form name="myform" action="/" method="post" onsubmit="document.getElementById('submit_button').disabled = 1;">
    
    <!-- form elements here -->
    
    </form>
    
    0 讨论(0)
提交回复
热议问题