Jump to content


Photo

PHP wont redirect page when form is submitted


  • Please log in to reply
14 replies to this topic

#1 skot

skot
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 27 March 2006 - 10:58 PM

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:
<?php

if($submit) {

header("Location: http://www.scott-hodson.com/bridgey/thanks.php");
exit;
}
?>

Error:
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


How can this be fixed? Thank you in advance.

#2 DougieB2

DougieB2
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 27 March 2006 - 11:08 PM

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?

#3 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 27 March 2006 - 11:11 PM

Try this:

<?php
ob_start();
if($submit) {

header("Location: http://www.scott-hodson.com/bridgey/thanks.php");
exit;
}

ob_end_flush();
?>


#4 skot

skot
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 27 March 2006 - 11:16 PM

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) View Post[/div][div class=\'quotemain\'][!--quotec--]
Try this:

<?php
ob_start();
if($submit) {

header("Location: http://www.scott-hodson.com/bridgey/thanks.php");
exit;
}

ob_end_flush();
?>
[/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 154


The 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]

#5 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 27 March 2006 - 11:23 PM

Aha! You MUST get rid of the

<!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>

YOU MUST! You should just echo the top, instead of leaving it plain html. Code before php code = bad.

#6 DougieB2

DougieB2
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 27 March 2006 - 11:24 PM

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.

<?php

if ($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

#7 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 27 March 2006 - 11:29 PM

I said that already... Oh well, as long as you're trying to help. ;)

#8 skot

skot
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 27 March 2006 - 11:44 PM

I moved the redirect above the html, but it still isn't redirecting after $submit

#9 DougieB2

DougieB2
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 27 March 2006 - 11:53 PM

post the source

#10 skot

skot
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 28 March 2006 - 12:03 AM

[!--quoteo(post=359074:date=Mar 27 2006, 06:44 PM:name=skot)--][div class=\'quotetop\']QUOTE(skot @ Mar 27 2006, 06:44 PM) View Post[/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?


<?php

if(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>";
}
?>


Full source here.


#11 DougieB2

DougieB2
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 28 March 2006 - 12:24 AM

your submit variable had no $, and you're sending output to the browser after the mail command. The redirect was also missing.

<?php

if($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 browser
header("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>";
}
?>


you also need to somehow set the $submit variable to true if there's $_POST data.

#12 skot

skot
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 28 March 2006 - 12:31 AM

Have adjusted code as you said but now the form won't even send, never mind redirect.

#13 High_-_Tek

High_-_Tek
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 28 March 2006 - 12:35 AM

<?php

if(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 browser
header("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>";
}
?>

Try that

#14 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 28 March 2006 - 12:54 AM

Try this for your "If" code:
<?php
if(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>';
}
?>

Ken

#15 skot

skot
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 28 March 2006 - 01:16 AM

A thousand thank you's, it seems to be working fine now :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users