0

I haven't found any solutions for my problem so I decided to ask. I would like to redirect my php script page after submitting a form, so I use header at the end of my codes! Interestingly, when I test it in my localhost, it works and redirects the page successfully, but, when I test it in my remote server it fails to redirect the page! and when I submit the form, it goes to the from processing script and show a blank page! I wonder if anyone can help me on this matter.

and here is my codes in the form processing script:

<?php require_once('Connections/db.php'); ?>


<?php 

if (isset($_POST['submit']))
{
$name = mysqli_real_escape_string($connection, $_POST['name']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$subject = mysqli_real_escape_string($connection, $_POST['subject']);
$message = mysqli_real_escape_string($connection, $_POST['message_area']);

$sql = "INSERT INTO contact_messages (name, email, subject, message)

VALUES ( '$name', '$email', '$subject','$message')";

$result = mysqli_query($connection, $sql);

if($result){
header('Location: http://www.mylink/index.htm'); 
exit();
} 
else{
    echo "ERROR: Could not execute the form " . mysqli_error($connection);
}
}
?> 
MASUMEH
  • 41
  • 6

3 Answers3

3
<?php require_once('Connections/db.php'); ?>


<?php 

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

Change your above code to

<?php require_once('Connections/db.php'); 

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

and also remove the last ?> if you do not have html after that.

After that, it should work.


To turn on Error Reporting, place ini_set('display_errors',1); error_reporting(E_ALL); at the beginning of your php script, and you should be able to see the error.

More info about the problem you are facing here

Community
  • 1
  • 1
viral
  • 3,724
  • 1
  • 18
  • 32
  • Dunno how this is supposed to help. Enclosing `require_once()` into its own PHP tags is valid. So is the closing PHP tag at the end. I don't see how this is supposed to solve the problem, especially if it runs on his local machine – Realitätsverlust Jun 16 '15 at 05:47
  • 2
    @YUNOWORK Because some servers will run the header redirect (Apache on Mac OS X I have found will ignore the whitespace), however most will not if there is output before the header, and there is a ton of blank output. – Rasclatt Jun 16 '15 at 05:48
  • But it is safe, if OP has space after `?>`. this can be also responsible for header already sent error. – viral Jun 16 '15 at 05:49
  • @Rasclatt Aaaah, ofc, the 2 lines between the first PHP tags ... I completely missed that. That's most likely the error, you're right. – Realitätsverlust Jun 16 '15 at 05:50
  • The header is all the way down the page. – Rasclatt Jun 16 '15 at 05:50
  • wow!! I could not believe that this solution works! but, it did! Thanks a lot @Viral. – MASUMEH Jun 16 '15 at 05:54
0

You are opening and closing the php tags each time on single php file. just remove the closing tag form first line as:

<?php require_once('Connections/db.php'); 

and also remove the <?php tag from second line.

sonam gupta
  • 775
  • 6
  • 17
0

Change the header look like this will work. Or

header('Location: index.htm'); 

Or

HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

<?php
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>