views:

466

answers:

4

I have a very basic form at http://www.happyholidaylites.com/contact.html and it is working great. When you submit the form, the user is brought to the index.html with no message that the form has been sent. I am looking to initiate an alert that says, "your form has been submitted" with an x button. My code looks like this:

      <form method="post" id="myForm" action="dynaform.php">

      <input type='hidden' name='rec_mailto' value='[email protected]'>
      <input type='hidden' name='rec_subject' value='New Contact Form'>
      <input type='hidden' name='rec_thanks' value='index.html'>

so on and so forth.....

The last line is what is telling the form what to do after the submit button is pressed, but I dont want it to point the browser to the index, rather I want a javascript popup with a success message. Any ideas?

A: 

Why not a simple onSubmit?

<form method="post" id="myForm" action="dynaform.php" onSubmit="alert('Thank you for your feedback.');" >
Dominic Bou-Samra
Wheres the gurantee at that point that the form has actually been submitted?
roryf
This really shouldn't be marked as the answer, as Rory state there is no guarentee that the form has been submitted. The onSubmit method is for pre-validating the form data and cancelling if necessary -1
James
i understand, but for my purposes, this is exactly what I wanted. I needed a workaround. I didnt need a solution that would work with the form. Rather just an alert regardless if the form is submitted or not.
JCHASE11
Yes but this solution is wrong. You are basically telling the user their submission was successful and you will contact them...when actually the submit request hasn't even been sent yet! So if they get this alert, they will think "ok its submitted and apparently I will be contacted shortly", close the dialog and could easily re-direct away from the page before the page is submitted. Setting yourself up for a bad fall in my eyes! Start as you mean to go on...
James
James, you are totally right, you deserve the answer...But this is all im looking for. THe form submits every time, without issue. I appreciate your words though.
JCHASE11
I guess as long as the solution works for you, just a heads up though in future I would definetly advise against it! Personally if I HAD to display an alert after submit I would write a custom function and call it OnSubmit of the form which would (via javascript) submit the form and then popup the alert message afterwords. This at least ensures the form was definetly submitted before the user was alerted.
James
A: 

Sounds like your PHP script handles the form submission by processing the input and redirecting the browser to the value in the rec_thanks field.

You can add something like onsubmit="YourJavaScriptFunction()" to the form tag to add client-side behavior prior to actually submitting the form. Within that function you can perform validation, use alert('Thank You!'), etc..

Mayo
Until form is processed, no way of knowing it was actually submitted...
roryf
+2  A: 

To be honest you are better re-directing to another page in order to avoid the user re-submitting the page on a refresh. Have a look at Post/Redirect/Get Pattern.

Popups can be extremely annoying on websites. You should create a page called "thank-you.html" that you can re-direct the user to on successful submission which has access to the site navigation options or even just do a re-direct back to the form page after a few seconds.

James
A: 

Instead of redirecting to index.html, redirect to thanks.html; your users will thank you because everybody hates popups!

Josh Stodola