How can radio buttons be effectively used in if-else statements in PHP for data selection?
When using radio buttons for data selection in PHP, you can effectively use them in if-else statements by checking which radio button is selected and then performing the corresponding action based on that selection. This can be achieved by accessing the selected radio button value in the PHP script and using conditional statements to execute the appropriate code block.
<?php
if(isset($_POST['submit'])){
$selectedOption = $_POST['option'];
if($selectedOption == 'option1'){
// Perform action for option 1
echo "Option 1 selected";
} elseif($selectedOption == 'option2'){
// Perform action for option 2
echo "Option 2 selected";
} elseif($selectedOption == 'option3'){
// Perform action for option 3
echo "Option 3 selected";
} else {
// Handle default case
echo "No option selected";
}
}
?>
<form method="post">
<input type="radio" name="option" value="option1"> Option 1<br>
<input type="radio" name="option" value="option2"> Option 2<br>
<input type="radio" name="option" value="option3"> Option 3<br>
<input type="submit" name="submit" value="Submit">
</form>