Jump to content

Contact Form Success Page


SuperPaperSam

Recommended Posts

I have a contact form that I placed in a modal popup (kind of like this one...)So when my form successfully sends the email the page gets redirected to another page called success.htmlBut that gets rid of the whole purpose of the in page popup (I don't want them to have to switch pages)Basically my question is what is the best way to tell the user that there email has been sent or not while staying on the page.

// send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");// redirect to success pageif ($success){  print "<meta http-equiv=\"refresh\" content=\"0;URL=success.html\">";}else{  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";}

Thanks

Link to comment
Share on other sites

Maybe I'm missing something. You got the popup from an AJAX tutorial. Keep using AJAX.I mean, you're not actually POSTING the form are you? You're using an XmlHTTP object?Or maybe what you learned was how to build the-popup, and you still need a basic AJAX tutorial?

Link to comment
Share on other sites

Sorry about that but I'm not using AJAX. That was just a example for people so they can understand what I'm doing.Basically I'm making a contact form that is sent to my email. But when they submit the form I want them (the user) to get some conformation text showing that it was sent. Right now it has a conformation page but it's on it's own page. And since I want them to stay on the same page (that's why I made the popup) I want to find a way to display conformation text on the popup.So depending on if the email was sent there should either be a "Your form was successfully sent." or "Sorry but there has been an error." that magicly (A.K.A. PHP) appears next to the "send" button.So the question is how do I change

if ($success){   print "<meta http-equiv=\"refresh\" content=\"0;URL=success.html\">"; } else{   print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">"; }

To something like

if ($success){   // Show Success text; } else{   // Show Error text; }

I think that clears everything up but if not just ask away. :)

Link to comment
Share on other sites

I want them to stay on the same page
Without using frames, AJAX is the only method that allows you to do that. Posting a form causes a new page to be loaded. There is no way around this. It doesn't matter if the form is in a temporarily hidden div. It's still a form and that's how forms communicate. The form goes out, a page comes back.AJAX was designed for the kind of communication you're describing: 2-way communication in the background that doesn't unload your existing document or download a new one.
Link to comment
Share on other sites

You would use ajax to submit the form data to PHP, which would validate the information and try to send the email and return information about whether that was successful or if there was an error, and you would use Javascript to get the response from PHP and show whatever you want on the page. It doesn't matter what the PHP script is doing, if you want to show a confirmation on the page then PHP needs to communicate back to Javascript whether it was successful, and Javascript needs to handle that. If you don't want the page to refresh then you need to use ajax for this.

Link to comment
Share on other sites

Of course you're posting a form. That's how you're getting this script to run:

// send email  $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");  // redirect to success page if ($success){   print "<meta http-equiv=\"refresh\" content=\"0;URL=success.html\">"; } else{   print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">"; }

The problem is that the instant this script outputs anything, it's going to replace your document. What we're suggesting is that you need to communicate with this script through an AJAX object instead. That will allow you to send back a response without replacing the document.Javascript does the heavy lifting in AJAX communication. Your PHP script can be very minimal. Send the mail, echo "true" for success, "false" for failure. Javascript reads that and puts up the appropriate message.But I suggest you build a test page to learn AJAX before you apply it to this document.
Link to comment
Share on other sites

Since I really don't want to deal with AJAX I want to compromise and use a javascript alert...

<script language="javascript" type="text/javascript"> alert('Your message was successfully sent!'); </script>

or

<script language="javascript" type="text/javascript"> alert('Sorry but there has been a error. Make sure you have a working internet connection and try again.'); </script>

But how would I put this in my PHP and make each javascript alert start when the email is sent or a error occurs.Please tell me exsactly what to do because im not to great with php.

Link to comment
Share on other sites

It's not a compromise. The result will still be a variation on your original. Posting a form will obliterate your current document. If that's the case, you might as well redirect the browser to a success or failure page. Forms technology is as old as the web. It's maybe more primitive than you want. But if you don't want to learn AJAX, you're stuck with it.An alternative is to reload the current document, but have PHP embed success or failure messages in a strategic location.Doing that would also mean revising the current document so that the success/failure message does NOT appear when the page first loads. In other words, your mail script would be in the same document that posts the mail form. This is actually a common technique. But you may need to study some more PHP to learn how to do that. To be frank, even with help, I don't think you're quite ready to do many of the things you want to do.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...