you_n_me_n_php Posted April 15, 2011 Share Posted April 15, 2011 Hi, I just want to put up a simple page that has an NDA to which someone can either "agree" or "disagree" and then automatically be forwarded to specific pages accordingly (google and yahoo are just there for testing). Problem is that I keep getting and "Undefined Index" for both "agree" and "disagree". What am I doing wrong? Here is my code: <form id="nda" name="nda" method="post" action=""> <input type="submit" name="agree" value="I agree" /> <input type="submit" name="disagree" value="I disagree" /> </form> <?php if($_POST['agree']){ header("Location: http://www.google.com"); } else if ($_POST['disagree']){ header("Location: http://www.yahoo.com"); } ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/233785-redirect-according-to-user-choice/ Share on other sites More sharing options...
QuickOldCar Posted April 15, 2011 Share Posted April 15, 2011 Nothing, and I mean nothing can be output before a header redirect. <?php if($_POST['agree']){ header("Location: http://www.google.com"); } else if ($_POST['disagree']){ header("Location: http://www.yahoo.com"); } ?> <form id="nda" name="nda" method="post" action=""> <input type="submit" name="agree" value="I agree" /> <input type="submit" name="disagree" value="I disagree" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/233785-redirect-according-to-user-choice/#findComment-1201921 Share on other sites More sharing options...
you_n_me_n_php Posted April 15, 2011 Author Share Posted April 15, 2011 thanks for the reply. i put everything as you have it here (and even before the doctype definition). I still get the same message regarding an undefined index for "agree" and "disagree" respectively... Quote Link to comment https://forums.phpfreaks.com/topic/233785-redirect-according-to-user-choice/#findComment-1202032 Share on other sites More sharing options...
Muddy_Funster Posted April 15, 2011 Share Posted April 15, 2011 could you post your form code please? this sounds like a name mis-match. Quote Link to comment https://forums.phpfreaks.com/topic/233785-redirect-according-to-user-choice/#findComment-1202038 Share on other sites More sharing options...
PFMaBiSmAd Posted April 15, 2011 Share Posted April 15, 2011 Your two $_POST variables won't exist until after the form has been submitted and referencing them before the form has been submitted will produce an undefined index error. You need to use isset to test variables that might not exist. You also need exit; statements after each header redirect to prevent the remainder of the code on the page from executing while the browser is performing the redirect - <?php if(isset($_POST['agree'])){ header("Location: http://www.google.com"); exit; } else if (isset($_POST['disagree'])){ header("Location: http://www.yahoo.com"); exit; } ?> <form id="nda" name="nda" method="post" action=""> <input type="submit" name="agree" value="I agree" /> <input type="submit" name="disagree" value="I disagree" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/233785-redirect-according-to-user-choice/#findComment-1202042 Share on other sites More sharing options...
you_n_me_n_php Posted April 15, 2011 Author Share Posted April 15, 2011 Awesome, PFMaBiSmAd! That got it done and also made me learn about 4-5 things. The first reply regarding where my redirect was located was the first thing to learn. You guys are great! That's why this thing is my home page. Thanks so much, again!!! Roland Quote Link to comment https://forums.phpfreaks.com/topic/233785-redirect-according-to-user-choice/#findComment-1202199 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.