Php - testing if a radio button is selected and get the value

后端 未结 7 702
温柔的废话
温柔的废话 2020-12-08 12:31

I\'m using php. I\'d like to know how can I test if a radio button is selected and get the value? i can test if the radio button is selected but i cannot get the value. I c

相关标签:
7条回答
  • 2020-12-08 13:01

    It's pretty simple, take a look at the code below:

    The form:

    <form action="result.php" method="post">
      Answer 1 <input type="radio" name="ans" value="ans1" /><br />
      Answer 2 <input type="radio" name="ans" value="ans2"  /><br />
      Answer 3 <input type="radio" name="ans" value="ans3"  /><br />
      Answer 4 <input type="radio" name="ans" value="ans4"  /><br />
      <input type="submit" value="submit" />
    </form>
    

    PHP code:

    <?php 
    
    $answer = $_POST['ans'];  
    if ($answer == "ans1") {          
        echo 'Correct';      
    }
    else {
        echo 'Incorrect';
    }          
    ?>
    
    0 讨论(0)
提交回复
热议问题