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
- In PHP programming, what are the best practices for structuring code using the "EVA" principle of Input -> Processing -> Output, and how can this improve code readability and efficiency?
- What are the potential pitfalls of using <ul> and [*] tags for displaying categories in PHP?
- What are the potential pitfalls of using nested queries in PHP, as seen in the forum thread example?