1

I have a form that saves data to a database with jQuery. On a success response I want to disable the submit button on the form so it can't be resubmitted. I've tried $(this).find("button[type='submit']").prop('disabled',true); but it's not working and I don't understand why. Can anyone tell me what's wrong?

Form

 <form action="" id="like_unlike" method="" enctype="multipart/form-data">
    <input type="hidden" name="file" value="<?php echo $page_id;?>" />
    <input type="hidden" name="userid" value="<?php echo $_SESSION['user_session'];?>" />
    <span> <button type="submit" id="like" class="btn btn-default btn-small" name="like"><span class="icon-heart-empty"id="heart"></span>
    <span class="submit-text" id="count"><?php echo $rowcount?></span></button></span>
    </form>

jQuery

$("#like_unlike").submit( function(e) { //Second Form
    e.preventDefault();
     var url = "update.php"; //Grab URL from form
     var formdata = $(this).serialize();
          console.log(formdata);   
        $.post(url, formdata, //Post the form
            function(response) {

    $('#count').html(response);
       $(this).find("button[type='submit']").prop('disabled',true);
              });
              });
     });
JulianJ
  • 1,259
  • 3
  • 22
  • 52

2 Answers2

0

I've noticed that you use this in

$(this).find("button[type='submit']").prop('disabled',true);

You should know that it doesn't refer to the form you want but ajaxSettings.

RBT
  • 24,161
  • 21
  • 159
  • 240
0

Remove extra }); from jquery code.

$("#like_unlike").submit(function(e) { //Second Form
e.preventDefault();
var url = "update.php"; //Grab URL from form
var formdata = $(this).serialize();
console.log(formdata);
$(this).find("button[type='submit']").prop('disabled',true);
$.post(url, formdata, //Post the form
  function(response) {
      $('#count').html(response);
    $(this).find("button[type='submit']").prop('disabled',true);
    
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" id="like_unlike" method="" enctype="multipart/form-data">
  <input type="hidden" name="file" value="1" />
  <input type="hidden" name="userid" value="23" />
  <span> <button type="submit" id="like" class="btn btn-default btn-small" name="like"><span class="icon-heart-empty"id="heart"></span>
  <span class="submit-text" id="count">Row Count</span></button>
  </span>
</form>
Other way you can disable button based on id like

$("#count").attr("disabled", "disabled");

xrcwrn
  • 5,339
  • 17
  • 68
  • 129