Clifton Miranda Clifton Miranda - 1 month ago 11
Javascript Question

How to assign variable for jquery ajax post method

How do i pass the radio button selected value to the gender.php page..
i am new to javascript.. lets say that i select male in the radio tag, how can i pass the value "male" to gender.php?

<div id="gender" class="selection-gender">
<input type="hidden" name="ad_id" value="xxx">
<input id="radio" type="radio" name="gender" value="both">Both
<input id="radio" type="radio" name="gender" value="female">Female
<input id="radio" type="radio" name="gender" value="male">Male<br>
</div>
<input id="submit_gender" type="submit" name="submit_choices" value="Submit">


<script type="text/javascript">
$(document).ready(function(){
$("#submit_gender").click(function(){

$.ajax({
type: 'POST',
url: 'gender.php',
success: function(data) {

$("#gender").html(data);

}
});
});
});
</script>

Answer

You can select the checked radio by this:

var selectedRadio = $("#gender").find("input[type=radio][name=gender]:checked").val()

Then you can post the data like this:

$.ajax({
    type: 'POST',
    url: 'gender.php',
    data: selectedRadio,
    success: function(data) {

        $("#gender").html(data);

    }
});
Comments