0

I created a disclaimer page which includes a Accept/Decline buttons and a Submit button. I would like it so that when someone selects Accept and then Submit it goes to a particular URL1. But if they select Decline it goes to URL2. All the code is below. The current problem is that when I select Accept or Decline then click Submit, it doesn't do anything.

<!DOCTYPE html>
<html>
<head>
    <title>Warning Page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<?php
session_start();

if (isset($_POST['Accept'])) {
    if ($_POST['Accept'] == 'Accept') {
        $_SESSION['did_accept'] = true;
        header ('Location: http://www.google.com');
        die('<a href="http://www.google.com">Click here to continue</a>');
    } else {
        header ('Location: http://www.yahoo.com');
        die('<a href="http://www.yahoo.com">Click here to continue</a>');
    }
}
?>

</head>
<body>
    <p>Some text</p>

    <label>
        <input type="radio" name="radio" value="Accept" id="Accept" onClick="Accept()">
    Accept</label>
    <br>
    <label>
        <input type="radio" name="radio" value="Decline" id="Decline" onClick="Decline()">
    Decline</label>
    <br>
    <input type="submit" value="Submit">

</body>
</html>
smartrahat
  • 5,381
  • 6
  • 47
  • 68
Michael
  • 13
  • 1
  • 8
  • What's the question/issue to be addressed? What doesn't work with what you have now? – Castaglia Mar 08 '16 at 23:15
  • Your radio buttons are named "radio" so they will show up as "radio" in your $_POST array – GordonM Mar 09 '16 at 16:42
  • I changed them back to name="Accept" – Michael Mar 09 '16 at 16:46
  • There a couple problems with your code. You need a `
    ` around your `input`s. You are using "radio" as the name of the radios so you should check for `$_POST['radio']`, not `$_POST['Accept']`. For example, `if ($_POST['radio'] == 'Accept')`
    – Cave Johnson Mar 09 '16 at 17:50

4 Answers4

0

To put radio buttons in a group, you have to give them the same name.

<label>
<input type="radio" name="Accept" value="Accept" id="Accept">
Accept</label>
<br>
<label>
<input type="radio" name="Accept" value="Decline" id="Decline">
Decline</label>
<br>
<input type="submit" value="Submit">

You don't need anything in the formaction of the button. Just make it type="submit" and it will submit to the PHP script in the action attribute of the form, which should be the script you showed at the top.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Something is wrong with my php code. To test, I used google.com for URL1 and yahoo.com for URL2 and the web page goes straight to yahoo every time it loads. – Michael Mar 09 '16 at 00:24
  • Does the form use `method="GET"` or `method="POST"`? The code in your PHP assumes it's `GET`. – Barmar Mar 09 '16 at 01:53
  • Where is the form that the button is supposed to submit? – Barmar Mar 09 '16 at 16:32
  • You can't use `header()` after outputting HTML. See http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Barmar Mar 09 '16 at 16:33
0

I modified my answer to give a better example. This is untested but should give you a good idea how it works.

<!DOCTYPE html>
<html>
<head>
<title>Warning Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<?php
session_start();

if (isset($_POST['acceptcheckbox']) && $_POST['acceptcheckbox'] == 'Yes') {
$_SESSION['did_accept'] = true;
header ('Location: http://www.google.com');
die('<a href="http://www.google.com">Click here to continue</a>');
}
if (isset($_POST['declinecheckbox']) && $_POST['declinecheckbox'] == 'Yes') {
header ('Location: http://www.yahoo.com');
die('<a href="http://www.yahoo.com">Click here to continue</a>');
}
?>

</head>
<body>
<p>Some text</p>

<form action="" method="POST" id="myForm">
<input type="checkbox" name="accept" id="acceptcheckbox" style="display: none" value="Yes">
<input type="checkbox" name="decline" id="declinecheckbox" style="display: none" value="Yes">
<label>
<input type="button" name="acceptbutton" value="Accept" id="Accept" onclick="CheckAccept()">
Accept</label>
<br>
<label>
<input type="button" name="declinebutton" value="Decline" id="Decline" onclick="CheckDecline()">
Decline</label>
</form>
</body>

<script>
function CheckAccept(){
document.getElementById("acceptcheckbox").checked = true;
document.getElementById("myForm").submit();
}
function CheckDecline(){
document.getElementById("declinecheckbox").checked = true;
document.getElementById("myForm").submit();
}
</script>

</html>
McCormick32
  • 185
  • 11
  • Where would I add the URL's for Accept and Decline? – Michael Mar 09 '16 at 00:21
  • It would be the same as you have it now, except instead of your PHP page looking for a GET variable it will be looking for a POST variable and the Accept / Decline checkbox value. – McCormick32 Mar 09 '16 at 00:28
  • You need to put your HTML buttons in a form with method="POST". This will send the elements to the page via a POST request. – McCormick32 Mar 09 '16 at 16:30
  • I just updated the javascript and tested it to ensure it works; however, the PHP I could not test at the moment but should work fine. – McCormick32 Mar 09 '16 at 17:43
0

Your issue is here:

if (isset($_POST['Accept']) && $_POST['Accept'] == 'Accept') {
    $_SESSION['did_accept'] = true;
    header ('Location: http://www.google.com');
    die('<a href="http://www.google.com">Click here to continue</a>');
} else {
    header ('Location: http://www.yahoo.com');
    die('<a href="http://www.yahoo.com">Click here to continue</a>');
}

As $_POST['Accept'] is not set on the first page load, as it is a GET request and not a POST request, you go to the else block which immediately redirects you.

Try something like this instead:

if (isset($_POST['Accept'])) {
    if ($_POST['Accept'] == 'Accept') {
        $_SESSION['did_accept'] = true;
        header ('Location: http://www.google.com');
        die('<a href="http://www.google.com">Click here to continue</a>');
    } else {
        header ('Location: http://www.yahoo.com');
        die('<a href="http://www.yahoo.com">Click here to continue</a>');
    }
}
Dezza
  • 1,094
  • 4
  • 22
  • 25
  • This fixed the issue of it going straight to yahoo.com, thanks. But now, when I select Accept/Decline and then Submit it doesn't do anything but I'm guessing that has to do with what Barmar and McCormick are talking about not having a form as part of my code.?.? – Michael Mar 09 '16 at 16:50
  • Yes, that is because you need a `
    ` so that the browser knows what to send, how to send it, and where to send it. See @Barmar and @McCormick32 for answers to that question
    – Dezza Mar 09 '16 at 16:53
0

Here is the code that I got to work just like I wanted it to based on my original post thanks to all the great advice I received from @Barmar, @McCormick32, @Dezza, and all the others:

<!DOCTYPE html>
<html>
<head>
<title>Warning Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<?php
session_start();

if (isset($_POST['Accept'])) {
if ($_POST['Accept'] == 'Accept') {
    $_SESSION['did_accept'] = true;
    header ('Location: https://www.google.com');
    die('<a href="https://www.google.com">Click here to continue</a>');
}
}
if (isset($_POST['Accept'])) {
if ($_POST['Accept'] == 'Decline') {
    $_SESSION['did_accept'] = true;
    header ('Location: http://www.yahoo.com');
    die('<a href="http://www.yahoo.com">Click here to continue</a>');
}
}
?>

</head>
<body>
<p>Some text</p>

<form method="post" action="">

<label>
<input type="radio" name="Accept" value="Accept" id="Accept" onClick="Accept()">
Accept</label>
<br>
<label>
<input type="radio" name="Accept" value="Decline" id="Decline" onClick="Decline()">
Decline</label>
<br>
<input type="submit" value="Submit">

</form>
</body>
</html>

Thank you!

Michael
  • 13
  • 1
  • 8
  • There is one small problem, I just found with the code above. On the webpage it allows you to select Accept and Decline at the same time instead of one or the other. Any ideas why? – Michael Mar 10 '16 at 15:48
  • Figured it out. It was because I changed the name of the second radio button. A radio group need to have the same name in order to select one or the other. – Michael Mar 10 '16 at 17:26