Jump to content

[SOLVED] header() not working in script


redpony

Recommended Posts

Hi,

 

I'm a complete php newbie. I've been searching for an answer to this before posting but I'm completely stumped. I have a basic form that inserts form data into a database. After it is successful, it echos a message that that the form was successful. Instead, I want to redirect to another page. This is a pure php page with no html in it.

 

Here is the code that works:

 

if ($errors == "") {

mysql_query("INSERT INTO registration2 VALUES(

'',

'".addslashes($_POST['firstname'])."',

'".addslashes($_POST['lastname'])."',

'".addslashes($_POST['email'])."'   

)") or die(mysql_error());

    echo "Registration Successful!";

 

  } else {

 

    echo $errors."Please go back and try again.";

 

  }

 

Here is the header code that doesn't:

 

  if ($errors == "") {

mysql_query("INSERT INTO registration2 VALUES(

'',

'".addslashes($_POST['firstname'])."',

'".addslashes($_POST['lastname'])."',

'".addslashes($_POST['email'])."'   

)") or die(mysql_error());

header("Location: thankyou.php");

exit; 

} else {

echo $errors."Please go back and try again.";

  } 

 

I've tried moving the header outside the braces and I've checked the code for extra spaces. My guess is that I need to approach this diffefently, but don't know how.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/43248-solved-header-not-working-in-script/
Share on other sites

Try this, it seemed to work for me.

<?php

if ($errors == "") {
mysql_query("INSERT INTO registration2 VALUES(
	'',
	'".addslashes($_POST['firstname'])."',
	'".addslashes($_POST['lastname'])."',
	'".addslashes($_POST['email'])."'    
	)") or die(mysql_error()); 
echo "Registration Successful!";

} else {

echo $errors."Please go back and try again.";

}

//Here is the header code that doesn't:

if ($errors == "") {
mysql_query("INSERT INTO registration2 VALUES(
'',
'".addslashes($_POST['firstname'])."',
'".addslashes($_POST['lastname'])."',
'".addslashes($_POST['email'])."'    
)") or die(mysql_error());
header("Location: thankyou.php");
} else {
echo $errors."Please go back and try again.";
} 

?>

 

I pretty much just took out that exit;

if you get the 'headers already sent' problem you should try the following:

 

<?php
ob_start(); ob_end_clean();
header("Location: thankyou.php");
?>

 

otherwise i would suggest you use, javascript:

 

<?php
// instead of header() use:
echo "<script> window.location.href = 'thankyou.php'; </script>";
?>

Archived

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

×
×
  • Create New...

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.