How do I get the value of a radio button in PHP?

后端 未结 5 947
遇见更好的自我
遇见更好的自我 2021-02-12 14:09

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

相关标签:
5条回答
  • 2021-02-12 14:26

    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
    ?>
    
    0 讨论(0)
  • 2021-02-12 14:29

    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>
    
    0 讨论(0)
  • 2021-02-12 14:39

    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"];
    
    0 讨论(0)
  • 2021-02-12 14:40

    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>
    
    0 讨论(0)
  • 2021-02-12 14:45
    <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?");
    }
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题