user1328273
user1328273

Reputation: 33

my php form does not redirect page to another page

I have a page with simple form. when I click on Submit button, i have some sql codes and at the end I need to redirect page to another page(contact.php).My sql codes are working fine and it stores in database, however it does not redirect to another page(to index.php) and shows the same page which has form(contact.php). I use the following code to redirect my page:

header('Location: index.php');

my form and php codes are as below:

  <form  name="contact" action="contact.php"  id="contact_form" method="post" >
 <input  type="hidden" id="ID" name="ID" value="<?php echo $yid ?>" />

 <textarea  id="Reason" name="Remark" placeholder="Write your Reason here" class="required" cols="10" rows="10"></textarea>


  <input class="button altbutton" type="submit" name="submit" value="submit" />

 </form>

 if (isset($_POST['submit'])) {

       $Remark = @$_POST ['Remark'];
       $ID= @$_POST ['ID'];

$query=" my sql code";
$result = mysql_query($query);

header('Location: index.php');

}

what should I do?or what I have missed? Thank you

Upvotes: 1

Views: 2616

Answers (5)

Vishal
Vishal

Reputation: 2181

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

<html>
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
?>

[reference]


Solution 1: In your file, move the PHP code to the begining (i.e. above <html> tag), and also do not echo anything before using header() function.

Solution2:
If you're having trouble to refactor your code, then you need to use Output buffering. Put ob_start() as the very first line of your php page, and use ob_end_flush() or ob_end_clean() after use of the header(). This effectively buffers all output, which allows you to call the header function before any output is actually printed. Note: remember to always exit or die() after setting a Location header.

Upvotes: 0

jondinham
jondinham

Reputation: 8499

The HTTP header "Location" don't seem to work properly always on all browsers (as per my experience). And if you have already 'echo'-ed some HTML (or text, whatever) before echoing the 'Location' header, the PHP function 'header' won't work.

Use this code:

<?php
$Js_Redirect = 
"<script>"+
"top.location = 'index.php';"+
"</script>";

echo $Js_Redirect;
exit();
?>

instead of

<?php
header('Location: index.php');
?>

Upvotes: 0

user1299518
user1299518

Reputation:

is there some html sent before the header? even whitespace?

also try this (from here)

$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");

Upvotes: 1

FrontEnd Expert
FrontEnd Expert

Reputation: 5813

   if(isset($your_set_values)
    {
         header("Location:index.php");
    }

if it doest seem to work :-

First try to find is there any error ?

using --

error_reporting(E_ALL & ~E_NOTICE);

if you didn't found any error .... try to print your query and what..

Upvotes: 0

Nick
Nick

Reputation: 6346

I'd imagine it's cause you have put it after output on the page, but without seeing your code this is just a stab in the dark.

To redirect to a new page you should use:

header("Location: index.php");
exit();

Also, make sure the above is placed before any output on the page, otherwise it won't work.

Doing the above, if you get an empty page, something has went wrong (check your error log or make sure error reporting is turned on).

Upvotes: 1

Related Questions