user1967599
user1967599

Reputation:

How to submit radio button value via jQuery post()

What I am trying to do is to grab the radio button value and submit it via jQuery post() into a php file and get a response.

The below code works fine when I'm trying to sent the value for the "city". As you can see the "test" is commented out. With the "test" being commented out the program works and the value for "city" gets submitted to the php file gets processed, returned and printed.

Could anybody spot the problem with getting the radio button value?

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#trans_type").click(function(){
    $('#realty_type').html("<b>Loading response...</b>");

    $.post("sql_s.php",
    {
      //  test: $('input[name=transaction_type]:checked', '#trans_type').val()
    city:"New York"
    },
    function(data,status){
    document.getElementById("object_type").innerHTML = data;
    });
  });
});
</script>

<meta charset="UTF-8">
</head>
<body>
<h1>Some title</h1><br />

<form action="" id="trans_type">
<b>What would you like?</b><br />
<input type="radio" name="transaction_type" value="2">Sell&nbsp;&nbsp;&nbsp;
<input type="radio" name="transaction_type" value="3">Buy
</form>
<div id="object_type">message comes here</div>


</body>
</html>

Upvotes: 0

Views: 565

Answers (2)

VIVEK-MDU
VIVEK-MDU

Reputation: 2559

I suggest to change that line

{
    test: $('input[name=transaction_type]:checked', '#trans_type').val(), 
    city:"New York"
},

replace into

 {
        test: $('input[name=transaction_type]:checked').val(), 
        city:"New York"
    },

No need to put that Current ID and also add comma separator (,) atlast your posted values for differentiate your values. Refer this JSFIDDLE

Upvotes: 1

Sergio
Sergio

Reputation: 28837

I supose the line you have in your code, commented, is what you want.
Just add a comma (,) since its a object and that is the separator you need...

So, use:

{
    test: $('input[name=transaction_type]:checked', '#trans_type').val(), // <- comma here
    city:"New York"
},

Upvotes: 0

Related Questions