Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Participant ,
Jun 17, 2012 Jun 17, 2012

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>

TOPICS
How to , Server side applications
54.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 18, 2012 Jun 18, 2012

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

...
Translate
Community Expert ,
Jun 17, 2012 Jun 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');

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 17, 2012 Jun 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 17, 2012 Jun 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 17, 2012 Jun 17, 2012

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 17, 2012 Jun 17, 2012

Yeah...  I actually figured that out while reading a bit more...  But what I need it to do is go to or get redirected to an *.html page...  Can this be done with the header tag?  (excuse my knowledge of PHP I've only been using it for two days now.) 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 17, 2012 Jun 17, 2012

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 17, 2012 Jun 17, 2012

I can't get it to work...  It seems like such a simple thing but it's just redirecting me to a contactlist.php page and sending an email (like it should) but I can't get it to redirect to an existing .html file in the same directory.  It just seems so lame to have the person have to click the "back" button on their browser to get back to the website.  I've tried using the "header" function in various locations...  Sometimes I get a warning message and other times I just get sent to .php page with no functionality.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 18, 2012 Jun 18, 2012

Can you repost the code with what you now have?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 18, 2012 Jun 18, 2012

I returned the code to the original way I had it since it wasn't working...  Basically, when you click submit on the contact info page it runs the .php script.  The script then runs and sends out the email w/ the contact information.  After that point it just leaves you on the .php script page with no where to go...  I would like it to either display a message and then redirect the user to another .html page after a short 5 or 10 seconds or by clicking another button on the .php page.  Everything I've tried with the "header()" command isn't working.  I either get an error message or I just get a blank page displaying the "header() and exit() functions.

Is there a way to redirect the user from the .php page back to an .html page?  (either by setting a timed delay or by clicking a button.)

Can you show me with the .php code I provided at the beginning of this discussion how it should work with the header() command (or any other method for that matter)?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 18, 2012 Jun 18, 2012

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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 18, 2012 Jun 18, 2012

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('someonesomeplace.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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 18, 2012 Jun 18, 2012

Works perfectly!!!  Thank you!  I was only moving part of the php code to the top of the page and sometimes not ALL the way up to the top but just before the <head> statement.  Now, I think I can do something with it.

One question though...  You mentioned something about the spambots...  Can they get at these docs too?  I've heard others mention spambots love to sort through your code for email addresses especially with a "mailto:" function call.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 18, 2012 Jun 18, 2012

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 18, 2012 Jun 18, 2012

That's interesting...  I was just reading up on the stripslashes() function.  If I want to add this security measure to my comments box ['comment']...  Is there an easy way to go about it?  I just tried a few different ways to use the function but the slashes still appear in the email message. For instance I tried:

$comment = stripslashes($_POST['comment']);  But the slashes were still in the comment box.  So, I'm assuming that both the magic quotes is turned on and I'm not using the stripslashes function properly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 18, 2012 Jun 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 18, 2012 Jun 18, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 20, 2012 Jun 20, 2012
LATEST

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

Alex Liebscher

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines