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

Link to comment
Share on other sites

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>

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.