Skip navigation
Currently Being Moderated

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

Jun 17, 2012 11:26 AM

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>

 
Replies
  • Currently Being Moderated
    Jun 17, 2012 12:19 PM   in reply to Prodigy9

    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');

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 17, 2012 2:47 PM   in reply to Prodigy9

    That error is most likely due to the location of the PHP script on the page.  Can you confirm that the PHP header code is before any HTML?

     

    Basically the PHP header is the equivalent of the HTML "head" code that can also redirect so if the HTML head tags exist that error gets generated.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 17, 2012 4:11 PM   in reply to Prodigy9

    Yes.  But instead of thinking of the form as a content block on your page, when you are working with the header tag think of it as, if the form was submitted redirect the user to a different HTML page, otherwise, load the page below.  The header would just say:

     

    header('Location: file.html');

     

    The extension doesn't matter all that much.  Even if it is doesn't have PHP code on it you could leave the final page as "file.php" and just render HTML on it.  PHP just tells the server if there is PHP code on the page execute it first, otherwise just render HTML. This way if you ever want to add any code later you always could.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 18, 2012 7:10 AM   in reply to Prodigy9

    Can you repost the code with what you now have?

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 18, 2012 8:53 AM   in reply to Prodigy9

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 18, 2012 9:56 AM   in reply to Prodigy9

    Yes.  But when it attacks these scripts it's called injection (google something like "prevent SQL injection").  Injection is where a bot tries to inject bad code into your script because data is not being validated.  One example of this, is a comments box.  Because you are not using stripslashes ( http://php.net/manual/en/function.stripslashes.php ) on the code, someone could put a single quote mark followed by PHP code.  When you use a function like stripslashes the code comments out quote marks so it doesn't think it's part of the script, sanitizing your data input and protecting your site.

     

    This is a basic example of something you should be aware of for security.  There are much better examples of how you should sanitize data, but it will take some time to learn and there are many more functions like that stripslashes that all have different functions like converting all special characters to HTML entities. 

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 18, 2012 11:08 AM   in reply to Prodigy9

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 20, 2012 6:40 PM   in reply to Prodigy9

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

     

    Alex Liebscher

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points