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>
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');
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
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.
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.
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)?
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?
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.
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.
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.
North America
Europe, Middle East and Africa
Asia Pacific