0

I have a one question radio button form. I want the answer to immediately redirect to page A if the answer is A or redirect to page B if the answer is B. I know that the answer probably relies on javascript, but I am fairly new to coding and javascript is my Achilles' heel.

I have the form selecting and a basic if else statement - just not sure how to tie the two together. Some other questions here were helpful, just not enough to fully help me put something together. Here is my current code:

<?php
echo '<form id="gate" action="index.php" method="post">';
echo '<p>question<br>';
echo '<input type="radio" name="gateway" value="A">answer A<br>';
echo '<input type="radio" name="gateway" value="No">answer B<br>';
echo '</p>';

// branching for different forms
if($gateway=="Yes") {
    header ('answer Yes url');
} else if($gateway=="No") {
    header ('answer No url');
}    
echo '</form>';
?>
</body>
</html>

I currently click on Yes or No and nothing happens. I would love some help connecting A and B to my statement. I would love to be able to do it without the user having to click Submit - have it redirect when they make the choice.

arielb
  • 392
  • 3
  • 10
learning_curve
  • 53
  • 2
  • 12

2 Answers2

3

First you should check if request is a POST, then fetch your 'gateway' from the post.

Note that after a redirect in php you have to stop the script with a die() or with exit().

Below is an example

if (isset($_POST)) {
    if ($_POST['gateway'] == "Yes") {
        header("Location: <YOUR YES URL>");
        die();
    } else {
        header("Location: <YOUR NO URL>");
        die();
    }
}

To achieve the form submit on checking the radio input, your best option would be to put onclick="this.form.submit();" on your radio inputs

  • I have edited the code to the following: `code` echo ' Yes
    '; echo ' No
    '; echo ''; if (isset($_POST)) { if($_POST['gateway'] == "Yes") { echo 'This sucks!'; // header('url'); die(); } else { echo 'I wish this would work!'; // header ('url'); die(); } } include '../footer.php'; `code`
    – learning_curve Mar 29 '19 at 16:50
  • It is working part way - the correct echos are showing when a choice is made, but url does not redirect and the footer at the end does not get included when commented instead of remarks. – learning_curve Mar 29 '19 at 16:50
  • 1
    Please make sure that you make the php redirect correct. It should be like this header('Location : https://google.com') – Marin Florin-Alexandru Mar 29 '19 at 17:48
  • You could check the answer from this question: https://stackoverflow.com/questions/2112373/php-page-redirect/2112394#2112394 – Marin Florin-Alexandru Mar 29 '19 at 17:54
  • Thanks! I am getting the headers already sent message so I will have to fix that. Hopefully that will be the final error. I appreciate all the help! – learning_curve Mar 29 '19 at 20:58
-1

you may try:

echo '<input type="radio" name="gateway" value="A" onchange="this.form.submit()">answer A<br>';

as suggested in Submit form on radio button click

noam gaash
  • 337
  • 2
  • 13