Redirect at end of php form script


I’m using the following php to write form info to a mysql database and then email an email address with this information. At the end of the form I’d like the user to be redirected to a new page (e.g., a thanks for submitting page).

Right now the database writing and emailing parts work fine.

I’ve tried using a hidden redirect field on the form html and using the header location php function but both didn’t work.

Any help is always greatly appreciated.


$con = mysql_connect(“localhost”,“dbname”,“password”);
if (!$con)
die('Could not connect: ’ . mysql_error());
}mysql_select_db(“dbname”, $con);$sql="INSERT INTO table


";if (!mysql_query($sql,$con))
die('Error: ’ . mysql_error());
echo "Your info has been recorded. ";mysql_close($con)


<?php // Contact subject $subject ="New info Received"; // Details $message=" Name: $_POST[name] Phone: $_POST[phone] Email: $_POST[email] Fax: $_POST[fax] Status: $_POST[status] Post Date: $_POST[postdate] "; $mail_from="[email protected]"; $header="from: EMAIL "; $to ='[email protected]'; $send_contact=mail($to,$subject,$message,$header); if($send_contact){ echo ""; } else { echo "ERROR"; } ?>


Sending an email may or may not trigger the headers to send, I’m not sure. What you can always do is use JavaScript to trigger a redirect.

Good call–thanks.

In your opinion, is using javascript ok? It seems there are several things that PHP just refuses to do and I’ve ended up using JS instead. Some people I talk to say stay away from JS, while others love it.

I don’t consider javascript to be the most graceful way to do something, but it works in most cases. I guess a meta redirect would work as well. Either way, you’re going to have to provide a redirect link as well just in case your visitors end up on the ‘redirect’ page and have their browser set to refuse being redirected by meta, and have javascript disabled.

Sponsor our Newsletter | Privacy Policy | Terms of Service