How to get switch value in PHP?

I created a basic website that requires the user to select a radio button. I want the PHP file to retrieve the value of the switch that was selected and respond appropriately, but currently the file does not produce any output. What is wrong with the code I'm using now? Why can't my PHP file get the switch value correctly?

Index.html:

<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 </form> <form method="GET" action="Result.php"> <input type="submit" value="Result" name="Result"> //This button opens Result.php </form> 

Result.php:

 <?php $radioVal = $_POST["MyRadio"]; if($radioVal == "First") { echo("You chose the first button. Good choice. :D"); } else if ($radioVal == "Second") { echo("Second, eh?"); } ?> 
+5
source share
5 answers

Two separate forms are used for your common input elements and one of them consisting only of a submit button.

Turn on 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> 
+7
source

This is the form that is being submitted. It has an action attribute that directs it to Result.php.

 <form method="GET" action="Result.php"> <input type="submit" value="Result" name="Result"> //This button opens Result.php </form> 

So that you can get the data you need in Results.php, you need to add 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 will also need to change your method to POST if you intend to use the superglobal $ _POST

 $radioVal = $_POST["MyRadio"]; 
0
source

First of all, you are doing it a little wrong. You use two forms to complete the task. Let me tell you how you can do this.

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 ?> 
0
source

You are using two separate forms for the html code, which means that the first form is not actually submitted when the button is clicked.

You do not need to change the PHP code in result.php , but ideally you should 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> 
0
source
 <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 the isset() function that sets this when you run your PHP code. In your code, you specify $radioVal = $_POST["MyRadio"]; where MyRadio is the undefined index for PHP. Here, when we submit the form, we submit the PHP code call without any lag, and you also use the double form. This is not true 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?"); } } ?> 
-1
source

All Articles