Reputation: 6975
I created a contact form that uses my php script to email the form parameters:
//File email.php
<?php
include('variables.php');
$to = "info@timeshare.org.uk";
$subject = "Quote From Website";
$name = $_POST["name"];
$email = $_POST["email"];
$number = $_POST["number"];
$resort = $_POST["resort"];
$headers = "From: ".$to."\r\n";
$message = "Name: ".$name."\r\n".
"Email: ".$email."\r\n".
"Number: ".$number."\r\n".
"Resort Name:".$resort."\r\n";
if (mail($to,$subject,$message,$headers)){
$replyHeader = "Thank You!";
$replyMessage = "Thank you for using our quick contact form. We will get back to you as soon as possible.";
}else{
$replyHeader = "Oops!";
$replyMessage = "An Error Occured whilst trying to send the form. Please try again later";
}
header( 'Location: http://localhost/TimeShare/formReturn.php' ) ;
?>
on submit the form action points to this code.
Now if the mail function is successfull, i want the form to redirect to formReturn.php, however i want the content in a particular div to show the $replyHeader and $replyMessage.
So once the form is posted, it redirects to a page that either displays a successfull message or an error message.
Now on my formReturn.php page ive tried including the variables Like so:
//File: formReturn.php
<body>
//Header code...
//sidebar code...
<div>
<?php include('php/email.php'); ?>
<h1> <?php echo $replyHeader; ?> </h1>
<p> <?php echo $replyMessage ?> </p>
</div>
//Footer code...
</body>
Problem with this code is, because im including the email.php file, i end up with a redirection loop. Which is bad!
So how would i get the variables $replyHeader and $replyMessage onto the page in that specific location depending on the success or failure of the mail() function
Upvotes: 0
Views: 593
Reputation: 4977
just update your email thing to send email and redirect ONLY IF
if (isset($_POST["email"])){
}
otherwise you know that form is not submitted and that you don't want to bother about sending email.
and as others suggested, you have to append some get param to the url, like Snicksie (header('Location: http://localhost/TimeShare/formReturn.php?success=[1|0]');
)
suggested. then on your script you check for that variable to see if you should show anything.
e.g.
if (isset($_GET["success"])){
if ($_GET["success"] == 1){
show success message
} else {
show error message
}
}
Upvotes: 0
Reputation: 18557
you could pass it as a get variable
<?php
include('variables.php');
$to = "info@timeshare.org.uk";
$subject = "Quote From Website";
$name = $_POST["name"];
$email = $_POST["email"];
$number = $_POST["number"];
$resort = $_POST["resort"];
$headers = "From: ".$to."\r\n";
$message = "Name: " . $name . "\r\n".
"Email: " . $email . "\r\n".
"Number: " . $number . "\r\n".
"Resort Name:". $resort . "\r\n";
if (mail($to,$subject,$message,$headers)){
$replyHeader = "Thank You!";
$replyMessage = "Thank you for using our quick contact form. We will get back to you as soon as possible.";
header( "Location: http://localhost/TimeShare/formReturn.php?header={$replyHeader}&message={$replyMessage}" ) ;
}else{
$replyHeader = "Oops!";
$replyMessage = "An Error Occured whilst trying to send the form. Please try again later";
header( "Location: http://localhost/TimeShare/formReturn.php?header={$replyHeader}&message={$replyMessage}" ) ;
}
?>
//File: formReturn.php
<body>
<div>
<h1> <?php echo $_GET['header'] ?> </h1>
<p> <?php echo $_GET['message'] ?> </p>
</div>
</body>
Upvotes: 0
Reputation: 1997
You can also add a GET-variable to your second page:
if($mail_is_succesfull)
header('Location: http://localhost/TimeShare/formReturn.php?success=true');
else
header('Location: http://localhost/TimeShare/formReturn.php?success=false') ;
Then you can add the message in your other page.
Upvotes: 2