This question already has an answer here:
I have a form in 'index.php`
<form action="" method="POST" id="myForm" name="myForm">
<input type="text" name="myInput" id="myInput" maxlength="9" onChange="runOnce();" />
</form>
and in my JS code I have:
function runOnce() {
var form = document.getElementById("myForm");
form.submit();
}
and I also have the JS to handle the submission
$(document).ready(function() {
$("#myForm").submit(function(event) {
// prevent page refresh
event.preventDefault();
$.ajax({
url: 'index.php',
type: 'post',
data: {
"myInput" : $('#myInput').val(),
},
success: function(response) {
alert(response);
}
});
});
});
and in my PHP section I have
if (isset($_POST["myInput"])) {
// do something. . .
}
The issue is that the page is still being refreshed even though i have event.preventDefault();
What am I doing wrong?
</div>