Skip to main content
Go7hic13
Known Participant
June 17, 2012
Answered

How do I redirect a page in PHP After User Submission?

  • June 17, 2012
  • 2 replies
  • 55423 views

Hi All...

I've created a basic comment/contact form and when I click the "Submit" button everything works fine and it sends off an email.  Perfect.  One problem though...  It redirects to a new page and it echos a small "Thank You!" line.  How do I get the user back to the contact form page on the web site (or any other page for that matter?  I'll paste the short/simple PHP code below...

Any direction on this topic would be appreciated.  Thanks!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Customer Inquiry</title>

</head>

<body><?php # Script 1.0 - contactlist.php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

 

          if (!empty($_POST['first']) && !empty($_POST['last'])  && !empty($_POST['email'])) {

                    $body = "First Name: {$_POST['first']}\nLast Name: {$_POST['last']}\nEMail Address: {$_POST['email']}\nContact Phone Number: {$_POST['phone']}\nContact Preference: {$_POST['contactvia']}\nBest Time To Contact: {$_POST['timepref']}\nComments:\n {$_POST['comment']}";

                    $body = wordwrap($body, 70);

 

                    mail('someone@someplace.net', 'NEW Customer Inquiry Submission',$body, "From: {$_POST['email']}");

 

                    echo '<p><em>Thank You for your Inquiry!  We will respond to you ASAP!</em></p>';

 

                    $_POST = array();

          } else {

                    echo '<p style="font-weight: bold; color: #C60">Please Fill Out The Form!</p>';

          }

}

?>

</body>

</html>

This topic has been closed for replies.
Correct answer Ben M

Here is the code for the .php file again in case you wanted to review it.  Like I said though I returned the code back to it's original state since I wasn't able to get it working.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Customer Inquiry</title>

</head>

<body><?php # Script 1.0 - contactlist.php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

          if (!empty($_POST['first']) && !empty($_POST['last'])  && !empty($_POST['email'])) {

                    $body = "First Name: {$_POST['first']}\nLast Name: {$_POST['last']}\nEMail Address: {$_POST['email']}\nContact Phone Number: {$_POST['phone']}\nContact Preference: {$_POST['contactvia']}\nBest Time To Contact: {$_POST['timepref']}\nComments:\n {$_POST['comment']}";

                    $body = wordwrap($body, 70);

                    mail('someone@someplace.net', 'NEW Customer Inquiry Submission',$body, "From: {$_POST['email']}");

                    echo '<p><em>Thank You for your Inquiry!  We will respond to you ASAP!</em></p>';

                    $_POST = array();

          } else {

                    echo '<p style="font-weight: bold; color: #C60">Please Fill Out The Form!</p>';

          }

}

?>

</body>

</html>


Ok first, thing, get rid of the $_SERVER['REQUEST_METHOD'].  Right now if a form is set to post at your address it will be processed.  Spam bots love these forms because you are not actually checking if your own form was submitted.  You should usually only need to check if $_POST['something'] is submitted.  I know I didn't say this before but I should have.

Next, move the <?php ?> to the beginning of the document before the <!DOCTYPE declaration.

Now, eliminate the "else" statement.

Next eliminate the "echo Thank you" line and the $_POST = array(); line.  $_POST is a superglobal and always an array so you should never have to define it. And if you are redirecting someone, there is nothing you need to echo.

So now your code should be down to:

<?php

if ( isset($_POST['submit']) && !empty($_POST['submit')) //Test if submit button named submit was clicked and not empty

{

          if (!empty($_POST['first']) && !empty($_POST['last'])  && !empty($_POST['email'])) {

                    $body = "First Name: {$_POST['first']}\nLast Name: {$_POST['last']}\nEMail Address: {$_POST['email']}\nContact Phone Number: {$_POST['phone']}\nContact Preference: {$_POST['contactvia']}\nBest Time To Contact: {$_POST['timepref']}\nComments:\n {$_POST['comment']}";

                    $body = wordwrap($body, 70);

                    mail('someone@someplace.net', 'NEW Customer Inquiry Submission',$body, "From: {$_POST['email']}");

                    header('Location: url.php');  //Redirect to new url if form submitted

     }

}

?>

<!DOCTYPE

Just move that "Please fill out the form" text to the actual page above the form.  Then if the user submitted the form, they will get redirected, if not they see the page.  Make sense?

2 replies

A_Liebscher
Inspiring
June 18, 2012

Actually, using the header function is not the only way. It would be an alternative to echo javascript to change the document location. Like so:

echo '

<script type="text/javascript">

document.location = "URL Where you want.com/example.php"

</script>

';

It's just another way!

Alex Liebscher

Go7hic13
Go7hic13Author
Known Participant
June 19, 2012

Hey...  Thanks I appreciate an alternative method.  It's nice to have choices!  I still have quite a bit to learn about PHP... 

A_Liebscher
Inspiring
June 21, 2012

No problem. I'm not sure if my way is any safer/faster/ more reliable though!

Alex Liebscher

Community Expert
June 17, 2012

Just use a PHP header to redirect the user instead of the echo ( http://php.net/manual/en/function.header.php ).

header('Location: /path/to/url.php');

Go7hic13
Go7hic13Author
Known Participant
June 17, 2012

I must be using this call incorrectly...  It's still sending the email out but I get push to a PHP error message (below).

Something I'm not understanding I guess...  Does this code go in the .php file or the .html file?  Any thoughts?

Thanks!

Warning: Cannot modify header information - headers already sent by (output started at /home/content/53/9285553/html/contactlist.php:8) in/home/content/53/9285553/html/contactlist.php on line 19

Go7hic13
Go7hic13Author
Known Participant
June 17, 2012

I think what I want it to do is to first submit the email form, display a message and then redirect it to an HTML page.

Will the "header" command make this happen?