How to redirect after submit the form?

my code is


// Set Site Email Addresses Here
/* This email must be one set up on your domain for most hosts and is the email to send mail from */
/* This email is your email to receive the contact form details and can be the same as the one above if required */
// ******************************************************************
// ******************************************************************
// Check for empty fields
if(empty($_POST['name'])      ||
   empty($_POST['email'])     ||
      empty($_POST['phone'])     ||
   empty($_POST['message'])   ||
   echo "No Values Provided!";
   return false;
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
$ipaddress = $_SERVER["REMOTE_ADDR"];   
// Create the email and send the message
$to = $siteemailtoreceive;
$email_subject = "Website Contact From:  $name";
$email_body = "You have received a new message from your website contact form.\n\r\n";
$email_body = $email_body."Here are the details:\n\r\n";
$email_body = $email_body."Name: $name\n\r\n";
$email_body = $email_body."Email: $email_address\n\r\n";
$email_body = $email_body."Phone: $phone\n\r\n";
$email_body = $email_body."Message:\n$message\n\n\r\n";
$email_body = $email_body."Sender's IP Address: $ipaddress\n\r\n";
$headers = "From: $siteemailtosend\n"; 
$headers .= "Reply-To: $email_address";   
return true;    

if ((mail($to,$subject,$message,$headers))&&(mail($autoTo, "Thank You from JDR Excavating", $autoreply, 'From:'))) {
   header("Location: ".$goto_after_mail);
   $success = "Your message has been sent. A JDR Excavating representative will be in contact with you shortly!";


You are missing the $goto_after_mail variable.

i changed to (“Location: http:/” $goto_after_mail);
it shows error HTTP ERROR 500

anyone helps? im a newbie…

either set the variable or remove it from the redirect.

If you entered the line exactly as above, there will be a syntax error because you’re missing the concatenation symbol . from between the string and the variable name.

What is in the $goto_after_mail variable? Is it in a correct format to be just stuck on the end there, or do you need a trailing / on the end of the hard-coded part? As @benanamen said above, you don’t seem to set it at all in the code you posted above.

Are you aware that you “sort of” send the email twice? Once in your standalone line, and then again preceded by your if() statement? (I’d show the code, but cannot copy/paste any more for some reason). The copy of the email that you send as part of your if() statement uses $autoTo and $autoreply variables, which also don’t seem to be defined anywhere. The first mail() function in that if() clause uses variables $subject and $message which don’t appear to be defined anywhere.

Also you have a return statement immediately after the first time you send the email. Because you’re not inside a function definition, this will end execution at that point so your redirect won’t run anyway.

Once you’ve fixed all that, stop using mail() and have a look at one of the other recommended methods of sending email from PHP.

1 Like

As mentioned above if you trigger header(…) before your $success message they will be taken to that location before you have chance to output the $success message.

Another way would be to instead use JS to redirect after a set time. So once the form has been submitted and the email sent show the success message along with ‘you will now be automatically redirected to XXX in the next 10 secs, If you aren’t redirected click here or cancel’ etc.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.