Jump to content

REDIRECT ACCORDING TO USER CHOICE


you_n_me_n_php

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/233785-redirect-according-to-user-choice/
Share on other sites

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>

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>

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

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.