The correct way to redirect after submitting the form

This may be a stupid question, but what is the right way to redirect the user to the success page after saving the form in the database?

I don’t know why, but if I add action="done.php" , the form will not save the data in my database.

I tried adding header ("location:/done.php"); , it worked, but when I moved the page to the source server (PHP 4 and MySQL 3.23.5), an error occurs when I try to submit the 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 by converting this .php file to UTF-8 without BOM .

Thanks everyone for the suggestions!

+6
redirect php submit forms
source share
6 answers

I fix by converting this .php file to UTF-8 without BOM .

Thanks everyone for the suggestions!

0
source share

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

The location header must have a space after the ":" and must be an absolute URI, similar to the following:

 header("Location: http://www.yoursite.com/done.php"); 
+6
source share

"Headers have already been sent," most likely means that there is some content in front of your PHP code in your .php file. It can be as simple as a space, or maybe your PHP code is embedded in HTML. In any case, make sure that nothing comes to your PHP code and everything will be fine.

Regarding the correctness of this redirection method, I consider this a common technique.

+4
source share

others responded with respect to the headers already submitted. An alternative is to include or request done.php if the update was successful. Do not forget to exit after switching on / necessary.

 if (mysql_query($sql,$con)) { header ("location:/done.php"); require_once('done.php'); exit(); } else { echo "Something is wrong"; } 
+2
source share

using ob_start(); at the top of the page and immediately after <?php

+2
source share

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

Is mysql an error?

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

+1
source share

All Articles