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;
}
?>
Related questions
-
flush function PHP scripts
-
escaping characters regular expressions errors
-
password hashing session management CSRF protection