Jump to content

Archived

This topic is now archived and is closed to further replies.

skot

PHP wont redirect page when form is submitted

Recommended Posts

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

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Try this:

[code]<?php
ob_start();
if($submit) {

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

ob_end_flush();
?>[/code]

Share this post


Link to post
Share on other sites
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]<?php
ob_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 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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
I moved the redirect above the html, but it still isn't redirecting after $submit

Share this post


Link to post
Share on other sites
[!--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]<?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>";
}
?>[/code]

[url=http://scott-hodson.com/bridgey/ebay/request.txt]
Full source here.[/url]

Share this post


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

[code]<?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>";
}
?>[/code]


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

Share this post


Link to post
Share on other sites
Have adjusted code as you said but now the form won't even send, never mind redirect.

Share this post


Link to post
Share on other sites
[code]
<?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>";
}
?>
[/code]

Try that

Share this post


Link to post
Share on other sites
Try this for your "If" code:
[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>';
}
?>[/code]

Ken

Share this post


Link to post
Share on other sites
A thousand thank you's, it seems to be working fine now :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.