I\'ve created a basic website that requires the user to select a radio button. I want a PHP file to retrieve the value of the radio button that was chosen and respond accordingl
First of all you are doing it a little wrong. You are using two forms to do the task. Let me tell you how can you do it.
index.html
<form action= "result.php" method="POST">
<input type="radio" name="MyRadio" value="First" checked>First<br> <!--This one is automatically checked when the user opens the page -->
<input type="radio" name="MyRadio" value="Second">Second
<br/>
<input type="submit" value="Result" name="Result"> <!--//This button opens Result.php -->
result.php
<?php
echo $_POST["MyRadio];
// on new page you will get "First" or "Second", depending on what you have selected on html page
?>
Your are using two separate forms for your general input elements and one consisting of a submit button only.
Include the submit button in the first form and it should work fine:
<form method="POST" action="Result.php">
<input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatically checked when the user opens the page
<input type="radio" name="MyRadio" value="Second">Second
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>
This is the form that's being submitted. It has an action attribute which directs it to Result.php.
<form method="GET" action="Result.php">
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>
In order for you to get the data you want in Results.php, you need to add the radio buttons to this form
<form method="POST" action="Result.php">
<input type="radio" name="MyRadio" value="First" checked>First<br>
<input type="radio" name="MyRadio" value="Second">Second
<input type="submit" value="Result" name="Result">
</form>
You're also going to need to change your method to POST if you're going to use the $_POST superglobal
$radioVal = $_POST["MyRadio"];
You are using two separate forms for the html code, which means the first form is actually not submitted when you press the button.
You shouldn't need to change the PHP code in result.php
, but you should ideally use one form.
<form method="POST">
<input type="radio" name="MyRadio" value="First" checked>First<br> //This one is automatically checked when the user opens the page
<input type="radio" name="MyRadio" value="Second">Second
<input type="submit" value="Result" name="Result"> //This button opens Result.php
</form>
<form method="post">
<input type="radio" name="MyRadio" value="First" checked>First<br> <!--This one is automatically checked when the user opens the page-->
<input type="radio" name="MyRadio" value="Second">Second
</br>
<input type="submit" value="Result" name="Result"> <!--This button opens Result.php-->
</form >
In my php code you can see that the function of isset()
that set that when your PHP code run. In your code you mention $radioVal = $_POST["MyRadio"];
where MyRadio is undefined index for PHP. Here when we submit the form then submit call the PHP code without any lag and you also use the double form. This is wrong for this code.
<?php
if (isset($_POST['Result']))
{
$radioVal = $_POST["MyRadio"];
if($radioVal == "First")
{
echo("You chose the first button. Good choice. :D");
}
else if ($radioVal == "Second")
{
echo("Second, eh?");
}
}
?>