redpony Posted March 18, 2007 Share Posted March 18, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/43248-solved-header-not-working-in-script/ Share on other sites More sharing options...
redpony Posted March 18, 2007 Author Share Posted March 18, 2007 OK, stoopid me didn't look at the sticky before posting. Adding ob_start(); at the top of the script fixed my problem. Maybe this reminder will help someone else--who doesn't read the sticky first. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/43248-solved-header-not-working-in-script/#findComment-209986 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 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; Quote Link to comment https://forums.phpfreaks.com/topic/43248-solved-header-not-working-in-script/#findComment-209987 Share on other sites More sharing options...
ignace Posted March 18, 2007 Share Posted March 18, 2007 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/43248-solved-header-not-working-in-script/#findComment-209988 Share on other sites More sharing options...
cmgmyr Posted March 18, 2007 Share Posted March 18, 2007 also you can use this: <?php //go to page.php in 2 seconds echo "<META HTTP-EQUIV=Refresh CONTENT=\"2; URL=page.php\">"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/43248-solved-header-not-working-in-script/#findComment-209989 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.