Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP Feedback form redirects

PHP Feedback form redirects
Thread Tools
Mac Elite
Join Date: Dec 2004
Status: Offline
Reply With Quote
Mar 25, 2006, 09:23 AM
 
I am having a problem with a PHP feedback form. I'm still learning PHP so please help.

I have a feedback form on a contact page. It works fine and sends an email to my inbox but I don't know how to create a redirect to a thank you page. I tried a solution I found on a wizard site but the redirect fails. In the mean time I have set up a simple page that has an echo script that says thank you. This is my feedback form in html

<form action="sendmail.php" method="POST">
<table border="0" cellpadding="8" cellspacing="8" summary="feedback form">
<tr><td><p>Your name :</p></td><td><input type="text" name="name" size="25" /></td></tr>
<tr><td><p>Your e-mail :</p></td><td><input type="text" name="email" size="25" /></td></tr>
<tr>
<td colspan="2">
<p>Message</p>
<textarea rows="15" cols="45" name="message"></textarea>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="Send Message">
<br />
</td></p>
</tr>
</table>
</form>


This is my PHP script

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//Save visitor name and entered message into one variable:
$formcontent="VISITOR NAME: $name\n\nFEEDBACK: $message";
$recipient = "supermario@my example blog.com";
$subject = "Message from Contact Page";
$mailheader = "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Failure!");
echo "Thank You! I'll be in touch with you soon";
?>


How do I fix the PHP script to direct the message sender to a thankyou.html page so they can carry on reading?
Anyone who denies climate changes naturally is a Climate Change Skeptic.
     
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Mar 25, 2006, 10:23 AM
 
header("Location: thankyou.html");
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
Mac Elite
Join Date: Dec 2004
Status: Offline
Reply With Quote
Mar 25, 2006, 11:25 AM
 
Before I try it is this OK?

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//Save visitor name and entered message into one variable:
$formcontent="VISITOR NAME: $name\n\nFEEDBACK: $message";
$recipient = "supermario@my example blog.com";
$subject = "Message from Contact Page";
$mailheader = "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Failure!");
header("Location: thankyou.html");
?>
Anyone who denies climate changes naturally is a Climate Change Skeptic.
     
Mac Elite
Join Date: Sep 2003
Location: London
Status: Offline
Reply With Quote
Mar 25, 2006, 02:54 PM
 
could use:

Code:
$mail_sent = mail($recipient, $subject, $formcontent, $mailheader); if($mail_sent==true){ header("Location: thankyou.html"); } else { //error handling }
that way you are sure the mail is sent.

I'd also suggest removing the \r from your code as some email checkers identify this as a virus attack.
     
Mac Elite
Join Date: Dec 2004
Status: Offline
Reply With Quote
Mar 25, 2006, 03:03 PM
 
Originally Posted by moodymonster
could use:

I'd also suggest removing the \r from your code as some email checkers identify this as a virus attack.
Thanks, learning more now. Bear with me as most of that was from sitewizard and I don't know what some variables like /r do.

What do /r and /n do?

If I remove /r and merge your code with what I have is this perfect now (without errors too)?

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//Save visitor name and entered message into one variable:
$formcontent="VISITOR NAME: $name\n\nFEEDBACK: $message";
$recipient = "supermario@my example blog.com";
$subject = "Message from Contact Page";
$mailheader = "From: $email\n";
$mailheader .= "Reply-To: $email\n";
$mailheader .= "MIME-Version: 1.0\n";
$mail_sent = mail($recipient, $subject, $formcontent, $mailheader);
if($mail_sent==true){
header("Location: thankyou.html");
} else {
//error handling
}
?>
Anyone who denies climate changes naturally is a Climate Change Skeptic.
     
Mac Elite
Join Date: Sep 2003
Location: London
Status: Offline
Reply With Quote
Mar 25, 2006, 03:10 PM
 
yes, that looks good.

replace
Code:
//error handling
with
Code:
echo 'error sending email';
that way it will give an error message.

// is just a way of leaving comments for humans in the code, ie the computer ignores anything prefixed with //. You can also use it to comment out code you don't want used. You can also use /* at the start of a section, and */ at the end to comment out a whole block of text/code.

\r and \n are different ways of entering a newline.

Personally I use:

http://phpmailer.sourceforge.net/

for email, a little while ago I needed to be able to send attachments from an online form. Using phpmailer was the easiest option.
     
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Mar 25, 2006, 04:14 PM
 
Originally Posted by Super Mario
Thanks, learning more now. Bear with me as most of that was from sitewizard and I don't know what some variables like /r do.

What do /r and /n do?

If I remove /r and merge your code with what I have is this perfect now (without errors too)?

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//Save visitor name and entered message into one variable:
$formcontent="VISITOR NAME: $name\n\nFEEDBACK: $message";
$recipient = "supermario@my example blog.com";
$subject = "Message from Contact Page";
$mailheader = "From: $email\n";
$mailheader .= "Reply-To: $email\n";
$mailheader .= "MIME-Version: 1.0\n";
$mail_sent = mail($recipient, $subject, $formcontent, $mailheader);
if($mail_sent==true){
header("Location: thankyou.html");
} else {
//error handling
}
?>

The \r and \n are carriage return and newline characters respectively... they essentially do the same thing, but are legacy carry overs from eras past. Well, \r is, \n is standard.

Your code looks good.
Although not a big deal by any means, you can make your success checking simpler.
if($mail_sent){
...
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
Mac Elite
Join Date: Dec 2004
Status: Offline
Reply With Quote
Mar 25, 2006, 04:32 PM
 
You guys have been great. One last question before I try the code. In my code I have:

$mailheader = "From: $email\n";
$mailheader .= "Reply-To: $email\n";

Why is there a period before = (.=) in a couple of lines? Also, why do some PHP scripts start <?php while others simply start with <?

If that doesn't need to be changed tomorrow I will try either:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//Save visitor name and entered message into one variable:
$formcontent="VISITOR NAME: $name\n\nFEEDBACK: $message";
$recipient = "supermario@my example blog.com";
$subject = "Message from Contact Page";
$mailheader = "From: $email\n";
$mailheader .= "Reply-To: $email\n";
$mailheader .= "MIME-Version: 1.0\n";
$mail_sent = mail($recipient, $subject, $formcontent, $mailheader);
if($mail_sent==true){
header("Location: thankyou.html");
} else {
echo 'Sorry there was an error sending your message. Please try again.';
}
?>

Or with the simpler mail_sent

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//Save visitor name and entered message into one variable:
$formcontent="VISITOR NAME: $name\n\nFEEDBACK: $message";
$recipient = "supermario@my example blog.com";
$subject = "Message from Contact Page";
$mailheader = "From: $email\n";
$mailheader .= "Reply-To: $email\n";
$mailheader .= "MIME-Version: 1.0\n";
$mail_sent = mail($recipient, $subject, $formcontent, $mailheader);
if($mail_sent){
header("Location: thankyou.html");
} else {
echo 'Sorry there was an error sending your message. Please try again.';
}
?>
Anyone who denies climate changes naturally is a Climate Change Skeptic.
     
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Mar 25, 2006, 04:49 PM
 
Originally Posted by Super Mario
You guys have been great. One last question before I try the code. In my code I have:

$mailheader = "From: $email\n";
$mailheader .= "Reply-To: $email\n";

Why is there a period before = (.=) in a couple of lines? Also, why do some PHP scripts start <?php while others simply start with <?
The period in PHP is a concatication character. It combines data together. For instance:
$string = "this is" . " a string" ;

makes $string = "this is a string". When you use the operator " .= ", it adds the data to the variable. Instead of setting the variable to something, you're adding to it.

So when you run this script, you get:
$mailheader = From: $email\nReply-To: $email\n

As for the delimiters <? and <?php , they both work, but <?php is better form.
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Mar 25, 2006, 04:50 PM
 
double..
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
Mac Elite
Join Date: Dec 2004
Status: Offline
Reply With Quote
Mar 27, 2006, 11:00 AM
 
Works great, thanks
Anyone who denies climate changes naturally is a Climate Change Skeptic.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:34 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2