How do I reset a form in an ajax callback?
Posted
by B.Gordon
on Stack Overflow
See other posts from Stack Overflow
or by B.Gordon
Published on 2010-03-22T02:53:22Z
Indexed on
2010/03/22
3:11 UTC
Read the original article
Hit count: 340
I am sending a form using simple ajax and returning the results in a div above the form. The problem is that after the form is submitted and validated, I display a thank you and want to reset the form so they don't just press the submit button again... Can't seem to find the right code to do this...
<form id="myForm" target="sendemail.php" method="post">
<div id="results"></div>
<input type="text" name="value1">
<input type="text" name="value2">
<input type="submit" name="submit">
</form>
So, my sendemail.php validation errors and success messages appear in #results without problems.
But... when I try to send back a javascript form reset command, it does not work. Naturally I cannot see it in the source code since it is an AJAX callback so I don't know if that is the issue or if I am just using the wrong syntax.
echo "<p>Thank you. Your message has been accepted for delivery.</p>";
echo "<script type=\"text/javascript\">setTimeout('document.getElementById('myForm').reset();',1000);</script>";
Any ideas gurus?
© Stack Overflow or respective owner