0

How can I change this function so when I submit form the button disables and page refreshes ?

function thanks() {
  setTimeout(function () {
    document.location.pathname = "index.php";
  }, 3000);
}

Page refresh function is working btw. I just need to disable button.

This is my form

<form  method="post" action="<?=htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8");?>?id={{$theme->id}}" id="myForm">
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  <input type="hidden" name="price" value="{{$theme->price}}">
  <input type="hidden" name="id" value="{{$theme->id}}">
  <button type="submit" class="btn btn-success" onclick="thanks()"><span class="glyphicon glyphicon-shopping-cart"></span>Buy theme</button>
</form>

EDIT

Problem is that whenever I disable the button the file doesn't start downloading.. why ?

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
frenchbaguette
  • 1,297
  • 6
  • 22
  • 41
  • _"so when I submit form the button disables and page refreshes"_ What is purpose of disabling button before page refreshes ? Is expected result for button to be disabled after page refreshes ? – guest271314 Jan 31 '16 at 20:26
  • 1
    Here's a related question: http://stackoverflow.com/questions/5691054/disable-submit-button-on-form-submit – Tom Prats Jan 31 '16 at 20:26
  • See http://stackoverflow.com/questions/29986657/global-variable-usage-on-page-reload/ – guest271314 Jan 31 '16 at 20:36

3 Answers3

0

Use the disabled property like this:

 document.getElementById("<button id>").disabled = true;
0

Try making it into an unobtrusive script:

<form  method="post" action="<?=htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8");?>?id={{$theme->id}}" id="myForm">
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  <input type="hidden" name="price" value="{{$theme->price}}">
  <input type="hidden" name="id" value="{{$theme->id}}">
  <button type="submit" class="btn btn-success" onclick="thanks()"><span class="glyphicon glyphicon-shopping-cart"></span>Buy theme</button>
</form>

And in jQuery:

$("#myForm").submit(function (e) {
  // do not allow to submit form
  e.preventDefault();
  // disable the submit button
  $(".btn.btn-success").prop("disabled", true);
  // do the set time out
  setTimeout(function () {
    document.location.pathname = "index.php";
  }, 3000);
});
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

On button click, set the disabled property of this button to true

HTML

<button type="submit" class="btn btn-success" onclick="thanks(this)">

jQuery

function thanks(elem) {
    $(elem).prop('disabled', 'disabled');
    setTimeout(function () {
        document.location.pathname = "index.php";
    }, 3000);
}
kapantzak
  • 11,610
  • 4
  • 39
  • 61
  • This almost works. Button disables and page refreshes, but the file doesn't start downloading. The problem is whenever I add disable function to button the file doesn't start downloading – frenchbaguette Jan 31 '16 at 20:30