I know there are other topics covering this, but I read through them, tried the solutions, and am still running into issues.
I have an array, $genderValues, that I loop through in order to echo two radio buttons inside of a form. The form has other inputs with validation. When the user checks male or female, I want that radio button to remain checked when they try to submit the form but fail to meet other validation, rather than having it clear itself.
$genderValues = ['Male','Female'];
function ending ($genderVal) {
if (isset($_POST['gender']) && ($_POST['gender'] == $genderVal)) {
$ending = " checked />";
return $ending;
}
else{
$ending = "/>";
return $ending;
}
}
foreach($genderValues as $genderVal){
echo "<label class='radio' for=" . $genderVal . ">" . $genderVal . "</label>
<input type='radio' name='gender' id=" . $genderVal . " value=" . $genderVal . ending($genderVal);
}
Change this line:
<input type='radio' name='gender' id=" . $genderVal . " value=" . $genderVal . ending($genderVal);
To this:
<input type='radio' name='gender' id=" . $genderVal . " value='" . $genderVal . "' " . ending($genderVal);