I have a HTML form in a wordpress site that sends data to a database. One of the fields asks if a person is male or female using a radio button. How ever even if the persons presses the "Female" radio button they get set to male (Status M)
I've attached my code, the <?php if( get_theme_mod( 'petition_zip' ) === true ) : ?>
is only a check in my wordpress theme to see if the field should be there. I first tried using only HTML but added some PHP later to try and remidy the problem, it did not so as far as I can tell the PHP attached is close to useless
<?php
$male_status = 'unchecked';
$female_status = 'unchecked';
if (isset($_POST['submit'])) {
$selected_radio = $_POST['zip'];
if ($selected_radio == 'M') {
$male_status = 'checked';
}
else if ($selected_radio == 'F') {
$female_status = 'checked';
}
}
?>
<?php if( get_theme_mod( 'petition_zip' ) === true ) : ?>
<div class="form-group">
<input type="radio" id="zip" name="zip" value="M" <?PHP print $male_status; ?>>
<label for="M">Male</label>
<input type="radio" id="zip" name="zip" value="F" <?PHP print $female_status; ?>>
<label for="F">Female</label>
</div>
<?php endif; ?>
I'm running queries on the database to confirm the status, everyone that signs up is put to status "M" (Male)
The error is possibly in your petition.sign.js file.
I would try to replace this line:
$zip = $( 'input[name="zip"]', this ).val();
with this
$zip = $('input[name=zip]:checked').val();
I managed to solve the issue by re-writing the PHP from scratch, how ever I do believe that my solution would not have worked without the input from @superhenke. I've attached the working PHP-code.
<?php
if (isset($_POST["submit"])) {
$male_status = "unchecked";
$female_status = "unchecked";
$other_status = "unchecked";
$selected_radio = $_POST["zip"];
if ($selected_radio == "M") {
$male_status = "checked";
}
elseif ($selected_radio == "F") {
$female_status = "checked";
}
elseif ($selected_radio == "other") {
$other_stauts = "checked";
}
}
?>
<?php if( get_theme_mod( 'petition_zip' ) === true ) : ?>
<p>Select your gender:</p>
<div class="form-group">
<input type="radio" id="male" name="zip" value="M" >
<label for="male">Male</label>
<input type="radio" id="female" name="zip" value="F">
<label for="female">Female</label>
<input type="radio" id="other" name="zip" value="O">
<label for="female">Other</label>
</div>
<?php endif; ?>