php mail form with redirect to same page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xmatter
Forum Newbie
Posts: 1
Joined: Wed May 19, 2010 2:34 pm

php mail form with redirect to same page

Post by xmatter »

I am new here, so if this topic was covered elsewhere, please direct me. thanks in advance!!!

here is my form (php side):

<?
function checkOK($field)
{
if (eregi("\r",$field) || eregi("\r\n",$field)){
die("Invalid Input!");
}
}

$name=$_POST['name'];
checkOK($name);
$email=$_POST['email'];
checkOK($email);
$phone=$_POST['phone'];
checkOK($phone);
$comments=$_POST['comments'];
checkOK($comments);

$to="sample@sample.com";
$message="Name: $name\r\nPhone: $phone\r\nEmail: $email\r\nMessage: $comments\r\n";
if(mail($to,"Information Request",$message,"From: $email\r\n")) {
echo "Your request has been sent. We will contact you within 2 business days";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
?>

After the submit button is clicked, i want the words "form submitted" displayed above the form, and to NOT have the user redirected to another page.
If possible, to also make the original form invisible (not show) and just display "form submitted".
I have searched for this and found free forms that offer this, but want to design my own and keep it simple for future use.
If anyone can direct me to a "simple" solution or form that offers this:
1)email verification
2)field validation
3)spam protection

please let me know.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: php mail form with redirect to same page

Post by Chalks »

xmatter wrote:1)email verification
2)field validation
3)spam protection
These are big topics. I recommend doing some searches for each of them and working out the solution on your own. That's by far the best method, and you'll learn more doing it that way.
xmatter wrote:After the submit button is clicked, i want the words "form submitted" displayed above the form, and to NOT have the user redirected to another page.
If possible, to also make the original form invisible (not show) and just display "form submitted".
To handle this, I prefer to have the form submit to a different page. That page handles the form data _silently_, then automatically redirects back to the original page with error code(s) and status. Based on those, you can choose whether or not to display the form. An easy way to pass information back and forth between pages is with sessions. If you really want to stay on the same page though, the principle is the same. Create a flag of some sort based on the submitted information and draw the form if flag is true, otherwise don't.
Post Reply