Success Page Redirect
I wrote a php contact form but I can't figure out how to redirect a user to a different page...
Here is my code:
<?php
if($_POST){
$name = $_POST['name']; //gets the entered name
$email = $_POST['email']; //gets the entered email address
$subject = $_POST['subject']; //gets the subject
$message = $_POST['message']; //gets the entered message
$headers = "From: $email \n"; //Set from email address
$to = "email@gmail.com"; //Set email address
//validating the fields if any empty
if($name != "" && $email != "" && $subject != "" && $message != ""){
mail($to,$subject,$message,$headers); //calling php mail function
} else
{
echo "Please fill in all fields and submit again!";
}
}
?>
<?php
$email_from = 'email@gmail.com.com';
$email_subject = "New Form submission - $subject"; // Change the message subject here
$email_body = "You have received a new message from $name ($phone) .\n Here is the message:\n $message. His/Her full contact information is: \n $name \n $email \n 1$phone.";
// Send The Email
$to = "email@gmail.com"; // Set valid email address to send the form to
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
?>
<M/>
Senior Poster
3,611 posts since Apr 2012
Reputation Points: 64
Solved Threads: 77
Skill Endorsements: 90
@<MICHAEL>
I wrote a php contact form but I can't figure out how to redirect a user to a different page...
What do you mean redirect a user to a different page? If a person submit the form it will post a message that the form has submitted.
I mean let me get this straight after when the user submit the form you want it to be redirected to a different page?
Is that correct?
If so then you need a header function to redirect the user after the form is submited:
<?php
header('Location: http://path/newpage.php'');
?>
LastMitch
Industrious Poster
4,118 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
You can write your redirection code right after
mail($to,$subject,$message,$headers); //calling php mail function
header("Location: page.php");
that will redirecy after sending the mail
rayidi
Junior Poster in Training
83 posts since May 2011
Reputation Points: 13
Solved Threads: 2
Skill Endorsements: 0
Wow thanks you guys, the information helped a lot!
Here is what I ended up using:
$url= "http://somewebpage.com";
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
But I began to think again, it doesn't look great when you get redirected to a new page. So my new thinking is how do you keep the user on the same page after they submit the form because normally if you submit, it'd take you to a blank page with a message on it... so maybe display a message on the form saying, "Thank you for submitting"? Can someone assist me with this next part?
So far, thanks for helping me out guys, now lets see if you guys can help me with phase 2!
<M/>
Senior Poster
3,611 posts since Apr 2012
Reputation Points: 64
Solved Threads: 77
Skill Endorsements: 90
I came up with the thought of not redirecting users to a new page because the contact form is going to be on every page (on the menu) and I don't want them to leave the pages they currently were on... So maybe I should put a message saying "Thank You For Submitting" after they submit the form (so they don't leave the page).
Anyways of doing that, I kind of need help with that because I can't find anything great off of google searches...
<M/>
Senior Poster
3,611 posts since Apr 2012
Reputation Points: 64
Solved Threads: 77
Skill Endorsements: 90
Well so far, it doesn't redirect you to a new page (which is what I want), but I don't get a message... why is that?
<M/>
Senior Poster
3,611 posts since Apr 2012
Reputation Points: 64
Solved Threads: 77
Skill Endorsements: 90
Shall I send you the link to the page so you can observe as well?
<M/>
Senior Poster
3,611 posts since Apr 2012
Reputation Points: 64
Solved Threads: 77
Skill Endorsements: 90
I sent you the link to my page, I am not sure if the code executed properly
<M/>
Senior Poster
3,611 posts since Apr 2012
Reputation Points: 64
Solved Threads: 77
Skill Endorsements: 90
Sorry, I updated the page, i now get the messages but it doesn't display properly, may you look again?
<M/>
Senior Poster
3,611 posts since Apr 2012
Reputation Points: 64
Solved Threads: 77
Skill Endorsements: 90
I am sorry, but i think i am just misreading this but where specifically would you put this:
<form>
<?php print (isset($errors) ? $errors : ''); ?><br />
<input></input>
</form>
And i will be sure to make some time to organize my files, I've got a lot of things on my plate in the moment but I will make sure that organizing my files is a completed task :).
<M/>
Senior Poster
3,611 posts since Apr 2012
Reputation Points: 64
Solved Threads: 77
Skill Endorsements: 90
I will update you on progress ;)
<M/>
Senior Poster
3,611 posts since Apr 2012
Reputation Points: 64
Solved Threads: 77
Skill Endorsements: 90