I have an HTML form that submits an email with PHP, and when it submits, it redirects to that PHP page. Is there a way to prevent this redirect from happening? I built an animation with jQuery that I want to occur instead of redirecting. I tried return false;, but it wouldn't work. Here's my function:
$(function() { $('.submit').click(function() { $('#registerform').submit(); return false; }); });
Here's the opening form tag:
<form id="registerform" action="submit.php" method="post">
And the submit button:
<input type="submit" value="SUBMIT" class="submit" />
Use the preventDefault() method on the event object to prevent a page refresh on form submit in React, e.g. event. preventDefault() . The preventDefault method prevents the browser from issuing the default action which in the case of a form submission is to refresh the page.
Use the return value of the function to stop the execution of a form in JavaScript.
You can use preventDefault method of the event object.
You should post it with ajax, this will stop it from changing the page, and you will still get the information back.
$(function() { $('form').submit(function() { $.ajax({ type: 'POST', url: 'submit.php', data: { username: $(this).name.value, password: $(this).password.value } }); return false; }); })
See Post documentation for JQuery
Instead of return false, you could try event.preventDefault(); like this:
$(function() { $('#registerform').submit(function(event) { event.preventDefault(); $(this).submit(); }); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With