i’m trying to send some radio buttons from a form with ajax, the problem is that i can’t get the buttons’ values.
Everything would be much much easier if i could just use
data: $('form#my_form').serialize(),
The problem is that that doesn’t send any values at all if the radio buttons are not checked and i need some kind of “not checked” value for some radio buttons groups in order to validate the form in php and reply with an error message.
That’s why i was trying with:
if ($('input[name=radio_group]').prop('checked')==true) {
var radio_value=$('input:radio[name=radio_group]').val();
}
else {var radio_value=0;}
and then just sending the single values.
But the function above doesn’t work: it returns the value as “0” if no radio buttons are checked but it keeps returning “0” even if they’re cheked, it only returns the right value if the first radio button is checked, otherwise it’s always “0”.
Any suggestions?
Also, is there a simpler way to achieve what i want? I’d only need .serialize() to send a null or whatever value if the radio buttons are not checked.
Thanks
Make sure all the radio buttons have the same name. May be they are not same that is the reason it is working only for the first radio button when it is checked. You can try this after ensuring the name of all the radio buttons