How can PHP be used to handle radio buttons for gender selection in a form?
To handle radio buttons for gender selection in a form using PHP, you can create a form with two radio buttons for male and female options. When the form is submitted, you can use PHP to check which radio button is selected and process the gender selection accordingly.
<form method="post">
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
$gender = $_POST['gender'];
if($gender == 'male'){
echo "You selected Male";
} elseif($gender == 'female'){
echo "You selected Female";
} else {
echo "Please select a gender";
}
}
?>
Related Questions
- How can PHP be used to ensure that each drop-down menu functions independently without interfering with each other?
- What are the potential security risks associated with using $_GET variables directly in SQL queries in PHP?
- How can the use of variable variables in PHP impact code readability and maintainability?