简体   繁体   中英

After form validation, how do you redirect to a new url w/ PHP

I'm very new to back-end web work and I'm trying to create a form for my theatre wherein someone can submit a space inquiry form. I have the form made, it works, but what I want it to do is after they hit 'submit' and the form is validated and emailed to us, they'll be redirected to a "thank you" page. I've tried using the behaviors in Dreamweaver CS3 but if I use onClick->goto URL, it will go, but if the page doesn't validate, I'll still get the incomplete warning and they have no chance to fix it. Right now, the validation works, but I don't have the onClick->goto URL in place and it just refreshes with a blank form.

Here's the PHP I've been using:

<?

if(sizeof($_POST)) {
$body = "";
while(list($key, $val) = each($HTTP_POST_VARS)) {
$body .= "$key: $val \n";
}

mail("rentals@woollymammoth.net", 
"Rental Request",
$body);


?>

here's the Javascript:

   <form action=<? echo $PHP_SELF; ?> method="post" name="rentalRequest"  onSubmit="MM_validateForm('firstName','','R','lastName','','R','organization','','R','emailAddress','','RisEmail','phone','','R','address1','','R','city','''R','guestNumber','','RisNum','timeStart','','R');return document.MM_returnValue">    

I've spent the better part of the day researching this online and cannot find an answer. Any help would be greatly appreciated. I'm willing to send cookies to whomever can help me out.

You have to send an HTTP Location header ; this can be done, in PHP, with code such as this example :

header('Location: http://www.example.com');
exit();

A couple of notes :

  • The header function must be called before any output is sent to the browser.
  • The exit() call makes sure that your script really ends immediatly after the call to header()
  • In theory, you should use an absolute URL (such as http://www.example.com/test.php ) , for the HTTP Location header -- even if a relative URL (such as test.php ) generally works in browsers.

In PHP, you can redirect using this code:

header("Location: thanks.php");

Add this after you have handled the form submission. For completeness, consider adding exit(); after the header() function to prevent any further execution.

您可以在php中使用header()函数来更改'location'变量,更多内容请参见http://php.net/manual/en/function.header.php :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM