talkingAnimals Posted October 27, 2009 Share Posted October 27, 2009 I am very very new to PHP and I need to create a simple contact form that requires only one field to be filled in. I found a tutorial to build it and it works great, I just want to make one modification. Instead of going to a blank page that outputs my thank you message, I want to redirect to a thank you page (an html file that is styled and looks like it's part of my site). I thought I would simply replace echo ("bla blah"); with header("location:url"); but it doesn't seem to work. <?php $email = '[email protected]'; $subject = 'Frolick - Email Sign Up'; $message = $HTTP_POST_VARS['message']; if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $message)) { echo "<h4>Invalid email address</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; } elseif ($subject == "") { echo "<h4>No subject</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; } elseif (mail($email,$subject,$message)) { echo "<h4>Thank you for sending email</h4>"; } else { echo "<h4>Can't send email to $email</h4>"; } ?> I want to replace this portion: elseif (mail($email,$subject,$message)) { echo "<h4>Thank you for sending email</h4>"; } else { echo "<h4>Can't send email to $email</h4>"; } With something like this: else if (mail($email,$subject,$message)) { header( "Location: thanks.html" ); } else { header( "Location: sorry.html" ); } But it's a no go... any ideas? Link to comment https://forums.phpfreaks.com/topic/179251-header/ Share on other sites More sharing options...
trq Posted October 27, 2009 Share Posted October 27, 2009 Don't echo anything before your call to header(), it doesn't really make any sense to do so anyway because all your going to do is redirect your user. Link to comment https://forums.phpfreaks.com/topic/179251-header/#findComment-945740 Share on other sites More sharing options...
taquitosensei Posted October 27, 2009 Share Posted October 27, 2009 modify thanks.html to whatever you want that matches. That's the right syntax not sure why it wouldn't be working. header("Location: yourthankyoupage.html"); Link to comment https://forums.phpfreaks.com/topic/179251-header/#findComment-945744 Share on other sites More sharing options...
knsito Posted October 27, 2009 Share Posted October 27, 2009 You cannot have any output before a header() redirect. So you should buffer your output, this way it will not be displayed unless needed. If your validation checks fail, flush your buffer. On success, clear your buffer and redirect. Untested below: <?php ob_start(); //Buffer your Output $email = '[email protected]'; $subject = 'Frolick - Email Sign Up'; $message = $HTTP_POST_VARS['message']; if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $message)) { echo "<h4>Invalid email address</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; } elseif ($subject == "") { echo "<h4>No subject</h4>"; echo "<a href='javascript:history.back(1);'>Back</a>"; } elseif (mail($email,$subject,$message)) { ob_clean(); //clear buffer [or perhaps ob_end_clean()] header("location:thankyou.html"); exit(); } else { echo "<h4>Can't send email to $email</h4>"; } //made it to the end so output your buffer ob_flush(); //or was it ob_end_flush() ?> Link to comment https://forums.phpfreaks.com/topic/179251-header/#findComment-945752 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.