How can the $_POST array be used to retrieve the selected radio button value in PHP?
To retrieve the selected radio button value using the $_POST array in PHP, you need to ensure that the radio buttons are within a form element with a method attribute set to "post". When the form is submitted, the selected radio button value will be available in the $_POST array with the name attribute of the radio button as the key.
<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'])){
$selectedGender = $_POST['gender'];
echo "Selected gender: " . $selectedGender;
}
?>
Keywords
Related Questions
- When faced with complex and outdated PHP code, what steps should be taken to ensure a successful migration process and avoid potential issues?
- What are the best practices for handling concurrent access to files in PHP to prevent data corruption or loss?
- What are some alternative methods to creating multiple arrays in PHP for different sensors?