Krishna
Krishna

Reputation: 157

Get data from input type radio using ajax

My problem with my code is that it returns the first value which is "male" and even if i click the female button it will still return the value "male". This is just my snippet..

func.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<input onchange="gen()" name="gender" type="radio" value="male" />Male
<input onchange="gen()" name="gender" type="radio" value="female" />Female
<script>
function gen(){
    $.ajax({
      type:"post",
      url:"ajax.php",
      data:{
         gender_val:$("[name=gender]").val(),
      },
      success:function(msg){
       alert(msg);
    }
    })
}
</script>

and my ajax.php goes like this

ajax.php

<?php
echo $_POST['gender_val'];
?>

Thank you in advance for your help.

Upvotes: 0

Views: 514

Answers (1)

Vandesh
Vandesh

Reputation: 6894

If you want the selected value , you can use this instead.

gender_val:$("input[name=gender]:checked").val()

Upvotes: 1

Related Questions