In this we need to know how radio button works.
All radio buttons in a single group will share same name.
In your case - gender
.
So, when we select any one option and submit the form, then only the selected radio button's value is stored in the gender
.
But as per the JavaScript/jQuery code you wrote, you are simply taking the value of each element and take it as the value for it's name.
In this case, you are not checking whether any radio button is selected or not.
You can try following code
data['gender'] = $('[name=gender]:checked').val();
If nothing is selected - the gender
property will be undefined.
You can change this logic as needed.
From the code it seems, you are iterating on all available field names. In that case you can update that like this
// The loop to get al form data
el.find('[name]').each(function(index, value) {
var el = $(this)
name = el.attr('name'),
value = el.val();
//make data object
data[name] = value;
//If this element is a radio button
if($(this).is(':radio')) {
data[name] = $('[name=' + name + ']:checked').val();
}
});