how to get radio button value using php example code

Example 1: radiobutton php controller example

<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';
}        
?>

Example 2: php code to submit a radio button value using onclick function

Foo: 
<input type='radio' name='foo' value='bar'> Bar <br>
<input type='radio' name='foo' value='fru'> Fru

Tags:

Php Example