Kaspars Milbergs
Kaspars Milbergs

Reputation: 784

Right way to redirect after form submit

Maybe its a stupid question, but what is right way to redirect user to the success page after form is saved in database?

I dont know why, but if I add action="done.php", than the form does not save data to my database.

I tried to add header ("location:/done.php"); it worked, but when I moved page to original server ( PHP 4 and MySQL 3.23.5 ) there is error when I am trying to send the form Warning: Cannot modify header information - headers already sent by ........

Here is my php code :

if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$adress = $_POST['adress'];
$post = $_POST['post'];
$phone = $_POST['phone'];

$sql="INSERT INTO tekstile_users (id, name, email, company, adress, post, phone)
VALUES
('', '$name','$email','$company', '$adress', '$post', '$phone')";

if (mysql_query($sql,$con)) {

    header ("location:/done.php"); 
    }
    else {
echo "Something is wrong";
}

}//end of submit button

I fix it converting that .php file to UTF-8 without BOM .

Thanks all for suggestions!

Upvotes: 6

Views: 32745

Answers (6)

xkeshav
xkeshav

Reputation: 54022

use ob_start(); at the top of page and just after <?php

Upvotes: 2

Kaspars Milbergs
Kaspars Milbergs

Reputation: 784

I fix it converting that .php file to UTF-8 without BOM .

Thanks all for suggestions!

Upvotes: 0

Scott
Scott

Reputation: 694

others have answered with respect to headers already sent. An alternative is to include or require done.php if the update was successful. Don't forget the exit after the include / require.

if (mysql_query($sql,$con)) {

    header ("location:/done.php");
    require_once('done.php');
    exit();
    }
    else {
echo "Something is wrong";
}

Upvotes: 2

qbert220
qbert220

Reputation: 11556

The "headers already sent" message means that your script has already output something. Is there anything displayed on the web page above this error message? Check for any whitespace before the <?php tag. Also check any include files for whitespace before or after <?php ?> tags.

The Location header should have a space after the ":" and should be a absolute URI, similar to the following:

header("Location: http://www.yoursite.com/done.php");

Upvotes: 6

jeroen
jeroen

Reputation: 91734

headers already sent means that something has already been sent to the browser.

Is mysql giving an error?

Also, note that $_POST was introduced in PHP 4.1 so if you are using a really old version of php, that might be causing an error.

Upvotes: 1

Ryan Kinal
Ryan Kinal

Reputation: 17732

"Headers already sent" most likely means that there is some content in your .php file before your PHP code. This could be as simple as white space, or maybe your PHP code is embedded in HTML. In either case, make sure nothing comes before your PHP code, and you should be fine.

As for the correctness of this method of redirect, I believe it is a generally accepted technique.

Upvotes: 4

Related Questions