views:

136

answers:

6

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?

A: 

Most forms nowadays overcome the problem of some users double-clicking buttons by doing a setAttribute('disabled', 'disabled') on the submit button. You may want to do a double-whammy of hiding the form completely, and putting a button in its place to unhide the form.

amphetamachine
I am doing validation in the php that sends the email... how would I set this attribute in the success data that I send back to the div?
B.Gordon
A: 

In the php file that outputs the results, stick a

echo '<p>Thank you. Your message has been accepted for delivery.</p>';

In there and tell the javascript to return that. Simple. It also makes it easier because if an error occurs, you just change the text outputted without having to change anything on the client side.

Henri Watson
A: 

I personally do this with Jquery, by setting the relevant forms fields to blank once the ajax submission returns. I think you'll find plenty of resources on it on StackOverflow or elsewhere if you decide to go down that route.

Tom
A: 

The following should work:

<script type="text/javascript">
  function resetForm() {
    document.getElementById("myForm").reset();
  }
</script>
Templar
For some strange reason... this does not do anything... no errors...just does not do anything.
B.Gordon
Did you call the function? You can add a button to test as follows:<input type="button" value="reset form" onclick="resetForm()">
Templar
Sorry, I think I misread your original question. I don't think you need to use setTimeout. If you just call the reset directly it should work:echo "<script type=\"text/javascript\">document.getElementById('myForm').reset();</script>";
Templar
I have added this echo to the data that gets placed in the div upon success... It does not do anything (and I cannot see the source since it is injected via ajax). But, the button method calls the script and clears the form so I know the function works... it just seems that the call may not be doing anything... maybe out of scope since it goes in after the page is already rendered?
B.Gordon
A: 

Real simple but maybe not the best way to go about it is after it valdidates do a header("Location: url?accepted=true") and then put if($_REQUEST['accepted'] == "true") echo "

Thank you. Your message has been accepted for delivery.

";

A: 

you'll need to eval() the ajax result once complete. if you are using something like jquery, this can be done very easily.

Fixtree
can you give me an example of where and how I would do this with the code in the problem here? Thanks for the input.
B.Gordon
$.ajax({ url: 'ajax/test.php', success: function(result) { eval(result); }});you could generate some javascript code inside test.php eg:alert("this is a test");
Fixtree