skot Posted March 27, 2006 Share Posted March 27, 2006 Hey. I'm using the following code to redirect the user to a thank you page once the information in a form is submitted. The form works fine and mails the info to me, but an erorr appears when it comes to the redirect.Code:[code]<?phpif($submit) {header("Location: http://www.scott-hodson.com/bridgey/thanks.php");exit;}?>[/code]Error:[i]Warning: Cannot modify header information - headers already sent by (output started at /home/me/public_html/bridgey/ebay/request.php:6) in /home/me/public_html/bridgey/ebay/request.php on line 154[/i]How can this be fixed? Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/ Share on other sites More sharing options...
DougieB2 Posted March 27, 2006 Share Posted March 27, 2006 if you use a header redirect you must do it before sending any output to the browser. where are you doing the redirect? are you printing any output beforehand? Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21366 Share on other sites More sharing options...
lpxxfaintxx Posted March 27, 2006 Share Posted March 27, 2006 Try this:[code]<?phpob_start();if($submit) {header("Location: http://www.scott-hodson.com/bridgey/thanks.php");exit;}ob_end_flush();?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21367 Share on other sites More sharing options...
skot Posted March 27, 2006 Author Share Posted March 27, 2006 The visitor fills in a form, the information in the form is then emailed to me. Then I would like the page to redirect to another page once the information is sent.I'm a bit of a beginner when it comes to PHP, so sorry if that doesn't answer your question.The page is here: [a href=\"http://www.scott-hodson.com/bridgey/ebay/request.php\" target=\"_blank\"]http://www.scott-hodson.com/bridgey/ebay/request.php[/a][!--quoteo(post=359063:date=Mar 27 2006, 06:11 PM:name=lpxxfaintxx)--][div class=\'quotetop\']QUOTE(lpxxfaintxx @ Mar 27 2006, 06:11 PM) [snapback]359063[/snapback][/div][div class=\'quotemain\'][!--quotec--]Try this:[code]<?phpob_start();if($submit) {header("Location: http://www.scott-hodson.com/bridgey/thanks.php");exit;}ob_end_flush();?>[/code][/quote]Tried that but the error is the same.Warning: Cannot modify header information - headers already sent by (output started at /home/scotthod/public_html/bridgey/ebay/request.php:6) in /home/scotthod/public_html/bridgey/ebay/request.php on line 154The code can be viewed here: [a href=\"http://scott-hodson.com/bridgey/ebay/request.txt\" target=\"_blank\"]http://scott-hodson.com/bridgey/ebay/request.txt[/a] Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21368 Share on other sites More sharing options...
lpxxfaintxx Posted March 27, 2006 Share Posted March 27, 2006 Aha! You MUST get rid of the [code]<!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=iso-8859-1" /><title>Instant Unlocking Service</title><style type="text/css"><!--.style1 { font-family: "Century Gothic"; font-weight: bold;}--></style></head><body bgcolor="#006699" text="#EAF9FF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"><p align="center"><font size="2" face="verdana"><b><img src="title.gif" width="621" height="49" /></b></font></p><p align="center"><b><font size="2" face="verdana">Thank you for purchasing an unlock code.</font></b><font size="2" face="verdana"><br />Please fill in the information below so that we can send you the right code for your mobile handset. </font></p> <p align="center"><font size="2" face="verdana"><b>[/code]YOU MUST! You should just echo the top, instead of leaving it plain html. Code before php code = bad. Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21369 Share on other sites More sharing options...
DougieB2 Posted March 27, 2006 Share Posted March 27, 2006 you must put your redirect at the top of your script, before you send any output to the browser. The above code suggestion buffers the output (ie doesn't send it) so it should work, but I'm guessing you've put if after you've already started sending output.move the if ($submit) ... code to before you're printing the <html> code.<?phpif ($submit) { header("Location: [a href=\"http://www.scott-hodson.com/bridgey/thanks.php");\" target=\"_blank\"]http://www.scott-hodson.com/bridgey/thanks.php");[/a]exit;}// else continue?><html><body>etc, etc, etc Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21370 Share on other sites More sharing options...
lpxxfaintxx Posted March 27, 2006 Share Posted March 27, 2006 I said that already... Oh well, as long as you're trying to help. ;) Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21373 Share on other sites More sharing options...
skot Posted March 27, 2006 Author Share Posted March 27, 2006 I moved the redirect above the html, but it still isn't redirecting after $submit Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21378 Share on other sites More sharing options...
DougieB2 Posted March 27, 2006 Share Posted March 27, 2006 post the source Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21380 Share on other sites More sharing options...
skot Posted March 28, 2006 Author Share Posted March 28, 2006 [!--quoteo(post=359074:date=Mar 27 2006, 06:44 PM:name=skot)--][div class=\'quotetop\']QUOTE(skot @ Mar 27 2006, 06:44 PM) [snapback]359074[/snapback][/div][div class=\'quotemain\'][!--quotec--]I moved the redirect above the html, but it still isn't redirecting after $submit[/quote]Right i've changed my mind. I was trying to get the page to redirect to cover up the fact that the form doesn't work correctly - now i'd like to fix it instead. [a href=\"http://scott-hodson.com/bridgey/ebay/request.php\" target=\"_blank\"]Click here[/a] to see the page as it is now. Notice the top, which shouldn't appear untill the form is sent. Also, everytime someone goes on that page, a blank form is sent to me, which I don't want happening untill the information is there and the visitor clicks the submit button. Any ideas?[code]<?phpif(submit) {@extract($_POST);$name = stripslashes($name);$email = stripslashes($email);$brand = stripslashes($brand);$model = stripslashes($model);$country = stripslashes($country);$op = stripslashes($op);$imei = stripslashes($imei);mail('*purposely_removed*','Unlock Request',"Brand: $brand \n Model: $model \n Country: $country \n Operator: $op \n IMEI: $imei \n Proof: $proof \n Email: $email","From: $name <$email>") or die("Error sending information. Please contact bridgey[at]gmail.com");echo("<font color=yellow><b>Thank you, your request has been sent. A reply will be sent to the email address you provided.</b></font>");} else {echo "<font color=orange><b>You must fill in all fields before sending.</b></font>";}?>[/code][url=http://scott-hodson.com/bridgey/ebay/request.txt]Full source here.[/url] Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21386 Share on other sites More sharing options...
DougieB2 Posted March 28, 2006 Share Posted March 28, 2006 your submit variable had no $, and you're sending output to the browser after the mail command. The redirect was also missing.[code]<?phpif($submit) {@extract($_POST);$name = stripslashes($name);$email = stripslashes($email);$brand = stripslashes($brand);$model = stripslashes($model);$country = stripslashes($country);$op = stripslashes($op);$imei = stripslashes($imei);mail('*purposely_removed*','Unlock Request',"Brand: $brand \n Model: $model \n Country: $country \n Operator: $op \n IMEI: $imei \n Proof: $proof \n Email: $email","From: $name <$email>") or die("Error sending information. Please contact bridgey[at]gmail.com");// don't send this text to the browser//echo("<font color=yellow><b>Thank you, your request has been sent. A reply will be sent to the email address you provided.</b></font>");// redirect the browserheader("Location: http://www.scott-hodson.com/bridgey/thanks.php");exit;} else {echo "<font color=orange><b>You must fill in all fields before sending.</b></font>";}?>[/code]you also need to somehow set the $submit variable to true if there's $_POST data. Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21392 Share on other sites More sharing options...
skot Posted March 28, 2006 Author Share Posted March 28, 2006 Have adjusted code as you said but now the form won't even send, never mind redirect. Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21395 Share on other sites More sharing options...
High_-_Tek Posted March 28, 2006 Share Posted March 28, 2006 [code]<?phpif(isset($_POST['submit'])) {@extract($_POST);$name = stripslashes($name);$email = stripslashes($email);$brand = stripslashes($brand);$model = stripslashes($model);$country = stripslashes($country);$op = stripslashes($op);$imei = stripslashes($imei);mail('*purposely_removed*','Unlock Request',"Brand: $brand \n Model: $model \n Country: $country \n Operator: $op \n IMEI: $imei \n Proof: $proof \n Email: $email","From: $name <$email>") or die("Error sending information. Please contact bridgey[at]gmail.com");// don't send this text to the browser//echo("<font color=yellow><b>Thank you, your request has been sent. A reply will be sent to the email address you provided.</b></font>");// redirect the browserheader("Location: http://www.scott-hodson.com/bridgey/thanks.php");exit;}else {echo "<font color=orange><b>You must fill in all fields before sending.</b></font>";}?>[/code]Try that Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21396 Share on other sites More sharing options...
kenrbnsn Posted March 28, 2006 Share Posted March 28, 2006 Try this for your "If" code:[code]<?phpif(isset($_POST['submit'])) { $to = 'xxxxx@example.com'; $subject = 'Unlock Request'; $body = array(); $errs = array(); foreach ($_POST as $key => $val) if ($key != 'submit') if (trim(stripslashes($val)) != '') $body[] = ucwords($key) . ': ' . trim(stripslashes($val)); else $errs[] = $key; $headers = 'From: ' . stripslashes($_POST['name']) . '<' . stripslashes($_POST['email']) . '>'; if (empty($errs)) { mail($to,$subject,implode("\n",$body),$headers) or die("Error sending information. Please contact bridgey[at]gmail.com"); echo '<p style="color:yellow;font-weight:bold">Thank you, your request has been sent. A reply will be sent to the email address you provided.</p>'; } else echo '<p style="color:orange;font-weight:bold">All fields must be filled in, The following fields were not filled in:<br />' . implode('<br />',$errs).'</p>';}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21404 Share on other sites More sharing options...
skot Posted March 28, 2006 Author Share Posted March 28, 2006 A thousand thank you's, it seems to be working fine now :) Quote Link to comment https://forums.phpfreaks.com/topic/5971-php-wont-redirect-page-when-form-is-submitted/#findComment-21412 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.