Hello there i want my button disabled after the form submitted.
Disabling the button works but it doesnt execute the php code.
I tried different scrips that are posted on the internet but they all do the same: disabling the button without executing the php code. So when form is submitted it needs to echo "test" but it doesnt echo "test".
When i delete the line "e.preventDefault();" then the button wont disable anymore and the echo still not displays
Code:
<!DOCTYPE html>
<html lang="en">
<body>
<h1>jQuery - How to disabled submit button after clicked</h1>
<form id="formABC" method="POST">
<input type="submit" name="submit" id="btnSubmit" value="Submit"></input>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<input type="button" value="i am normal abc" id="btnTest"></input>
<script>
$(document).ready(function () {
$("#formABC").submit(function (e) {
//stop submitting the form to see the disabled button effect
e.preventDefault();
//disable the submit button
$("#btnSubmit").attr("disabled", true);
//disable a normal button
$("#btnTest").attr("disabled", true);
return true;
});
});
</script>
<?php
if (isset($_POST['submit'])) {
echo "test";
}
?>
</body>
</html>
My Answer:
The issue here is that you are preventing the default form submission behavior with `e.preventDefault();`, which means that the form data is not actually being submitted to the server. Instead, you are just disabling the button and not allowing the form to be submitted.
To fix this issue, you can remove `e.preventDefault();` from your code and simply disable the button after the form is submitted. Here is an updated version of your code:
html
jQuery - How to disabled submit button after clicked
if (isset($_POST['submit'])) {
echo "test";
}
?>
With this code, the form will be submitted as usual and the button will be disabled after the form is submitted. The PHP code will also execute and echo "test" if the form is submitted.
Rate this post
5 of 5 based on 7097 votesComments