artsyL
artsyL

Reputation: 49

trying to pass a jquery variable into a query string

I have two radio buttons for which I want to get the checked value and then pass to a url query string:

here is the html

<h3>Please select an audience</h3>
  <p><input class="age" type="radio" name="ageLimit" value="allAge"/> All ages </p>
  <p><input class="age" type="radio" name="ageLimit" value="mature"/> Mature</p>

This is the js:

when I use this, the database pulls [object, Object]

url : 'upload.php?aud='+$('input[name="ageLimit"]:checked', '#myForm').val($('input:radio[name="ageLimit"]:checked', '#myForm')),

when I use this it only pulls the first variable

url : 'upload.php?aud='+$('input[name="ageLimit"]:checked', '#myForm').val(),

How do I get the checked variable to pass into the query string? I have looked at several similar questions on SO, but nothing treats a query string

Upvotes: 0

Views: 1041

Answers (3)

artsyL
artsyL

Reputation: 49

It turns out my problem was somewhere entirely else and the uploader needed to evaluate/pass the radio button. Gahhhhh. Thanks for all the help! –

here's the link: Why am I getting "undefined" passed to the db? jquery php plupload

var uploader = $('#uploader').plupload('getUploader');

uploader.bind('BeforeUpload',function(upldr,file){
upldr.settings.url = 'upload.php?aud=' + $('input[name="ageLimit"]:checked', '#myForm').val();
// here some runtimes might need a upldr.refresh(); (Though I'm not sure, I guess I remember Flash would.)
}
);

Upvotes: 0

Brian Minnich
Brian Minnich

Reputation: 402

Snaps to serialize above. I personally enjoy ajaxForm plugin (http://malsup.com/jquery/form/).

If your goal is to get the selected radio value into a url then play around with it on jsfiddle (link below).

http://jsfiddle.net/NQ6wH/

<h3>Please select an audience</h3>
<form>
  <p><input class="age" type="radio" name="ageLimit" value="allAge"/> All ages </p>
  <p><input class="age" type="radio" name="ageLimit" value="mature"/> Mature</p>
</form>
<button id="testMe">Test Me</button>

$('#testMe').click(function(e){
    e.preventDefault();
    var url = 'upload.php?aud=' + $('input[name=ageLimit]:checked').val();
    alert(url);
});

Upvotes: 1

user229044
user229044

Reputation: 239402

Use jQuery's serialize to zip your form up into a query string for you:

$('#myForm').serialize()

or

$('input[name="ageLimit"]:checked', '#myForm').serialize()

When you convert a jQuery object to a string, that's what you get, [Object object]. Your first example sets the value of your form element, and then returns the jQuery object so that you can chain additional methods on it.

Your second attempt, with val(), returns the value you're after. If you want to get several values, you'll have to select the elements, and then map them to their value, and then join the resulting array. But you shouldn't do this. You should use serialize to do it for you.

Upvotes: 4

Related Questions