Ajax and checkboxes

Hello,

I’ve done a bit of searching on this topic but cannot seem to find what I need.

I’m basically doing a form submit through ajax and need to figure out how to deal with checkboxes.

My script is simple, when I hit submit It just grabs the form element’s values and sends them along to the php file on the server.

But how does one deal with checkboxes? I’m assuming it’s something like cycling through the form, grabbing the value from each checkbox that is checked, storing it in an array, sending it to the php file to be parsed out, but how do I do that?

Any tips for solving this quickly would be great. I’ve been avoiding this problem and working around it for months, but now since I’m not doing live editing, but form simulation I need a real solution.

I’m using jquery, for what it’s worth.

Thanks!

Take a look at jquerys serialize() method.

awesome, thanks!

Maybe there’s an obvious answer to this, but I’ve been accessing variables passed to the server using the $_GET method. How would that work if I have multiple variables with the same name?

the example on visualjquery is like this: &check=check1&check=check2

probably obvious… :slight_smile:

php supports a handy feature if you name your request variables a certain way.
http://www.php.net/manual/en/faq.html.php#faq.html.arrays

ah, of course. Thanks!

And out of curiosity, how would I check to see if a specific checkbox id checked accessing it by it’s id?

I know in prototype it was something like if($(‘checkbox_id’).checked == true) or something like that. Seems like all the checkbox related functions in jquery are for counting.

There’s lots of ways, but, did you read the documentation for serialize()? It does that for you because it only serializes successful controls.

But, if you want access to the underlying dom element
$(“#checkbox_id”)[0].checked
(the query object can be accessed like an array, and for an id selector, the element will always be the first and only element)
You could also
$(“#checkbox_id”).is(“:checked”)

I did. I guess I was a little reluctant to change the way my script is submitting data. But no big deal really. I’m going to roll with it. thanks for your help!

great, actually got it working already, big thanks.

I’m actually still having issues…

this is being sent to the server:

do_this=update_image_data&image_title=Title+for+this+image+here&image_summary=this+is+the+summary+for+this+image&image_content=content+goes+here&image_meta_title=meta+title&image_meta_alt=meta+alt&categories%5B%5D=2&categories%5B%5D=4&products%5B%5D=1

A print_r of $_GET returns:


Array
(
    [do_this] => update_image_data
    [image_title] => Title for this image here
    [image_summary] => this is the summary for this image
    [image_content] => content goes here
    [image_meta_title] => meta title
    [image_meta_alt] => meta alt
    [categories] => 
    [products] => 
)

Categories are the values of checkboxes, that are stored in my html file with names name=“categories”, and they have values set…

Am I missing something? Thanks for any help.

When you have a problem you’re trying to debug, it’s often a good idea to try to isolate the problem as much as possible.

Try making a new php file with only this


<?php
print_r($_GET);

I bet it works properly.

Ah, it was actually my browser’s cache. Thanks again. :slight_smile: