Reputation:
I have a form in html with several fields, one of which is a radio button.When I submit the form into a php page I want to display the contents of each field and the name of the radio button selection. However the radio button doesn't display the name property but echoes "on" when selected and nothing when not, what can I do to show the name of the selection?
this is the html page
<html>
<body>
<form action="confirmdata.php" method="post">
Name <input type="text" name="name"><br><br>
Gender <input type="radio" name="gender" value="Male">Male<input type="radio" name="gender" value="Female">Female<br><br>
Email <input type="text" name="email"><br><br>
Inform email <input type="checkbox" name="inform"><br><br>
<input type="submit">
</form>
</body>
</html>
and this is the php page
<html>
<body>
<?php
foreach($_POST as $data){
echo $data; ?>
<br>
<?php
}
?>
<a href="registrationform.html">Return to registration form</a>
</body>
</html>
Upvotes: 0
Views: 1102
Reputation: 144
I think you want display the selected values after submit the form. so $_POST['gender']
is holding the value either 'Male' or 'Female', so after submit the form add condition to check $gender
is male or female then use checked
attribute inside a radio button tag.
Please follow the below simple steps.
$gender = $_POST['gender'];
if ($gender == 'Male') {
?>
<input type="radio" name="gender" value="Male" checked>Male
<?php
}
else {
?> <input type="radio" name="gender" value="Female" checked>Female<br><br>
<?php
}
Upvotes: 0
Reputation: 27723
I'm guessing that, maybe this might help you to figure it out:
$html = '';
foreach ($_POST as $data) {
$html .= '<input type="radio" name="gender" value="' . $data . '"> ' . $data . '<br>';
}
echo $html;
It seems you have your form values in the $data
. When you loop through it, it will fill the values back in the input radio elements that you have. You might also need to add the keys to name attributes:
$html = '';
foreach ($_POST as $key => $data) {
$html .= '<input type="radio" name="' . $key . '" value="' . $data . '"> ' . $data . '<br>';
}
echo $html;
Upvotes: 1