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
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;

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.