What is the correct syntax for handling radio buttons in PHP forms?
When handling radio buttons in PHP forms, you need to ensure that only one option can be selected at a time. This can be achieved by giving each radio button the same name attribute and different values. When the form is submitted, you can use PHP to check which radio button was selected by accessing the value of the selected option.
<form method="post" action="process_form.php">
<input type="radio" name="color" value="red"> Red<br>
<input type="radio" name="color" value="blue"> Blue<br>
<input type="radio" name="color" value="green"> Green<br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$selected_color = $_POST['color'];
echo "You selected: " . $selected_color;
}
?>
Keywords
Related Questions
- How can PHP users effectively utilize the Gzip functionality in conjunction with tar?
- What MySQL function can be used to determine the week number of a given date for creating weekly statistics in PHP?
- How can the use of comma-separated sections in the exif_read_data function impact the retrieval of specific data like thumbnails and comments from images?