0

I need to have all the values of checked checkboxes sorted with commas in javascript, so I can send the to a form sending php script.

The javascript:

var services = [];
$('#field-services:checked').each(function() {
services.push($(this).val());
});

$.post(rootUrl+'/wp-admin/admin-ajax.php', { action:"two2_send_contact_form", services:services }

I have the checkboxes inside a div with the id field-services the php that send the email

$services = $_POST["services"];
$subject = "BLAH BLAH";
    $body = "Services: $services, \n\n$message";
3
  • jquery I presume? Probably an important tag... Commented Aug 2, 2012 at 4:21
  • doesnt seem like the case, everything else works perfect, and dreamweaver didnt detect anything. i'll double check anyway thanks Commented Aug 2, 2012 at 4:37
  • I'm saying, you didn't tell us what javascript library you're using. Your question tags don't include "jquery". That seems more important than "checkbox". Commented Aug 2, 2012 at 4:40

1 Answer 1

2

$('#field-services:checked') of course won't work, because id must be unique so there must be only one #field-services checked. You probably want to do:

var services = []
$('#field-services input:checked').each(function(){
  services.push(this.value)
})
Sign up to request clarification or add additional context in comments.

3 Comments

i tried this, and the result i got in the email was: "Array, "what i was getting before was simply " , ". i'm not sure how the "Array" got in there
Arrays become 'Array' when you cast them to a string. Try using implode(', ',$services) in the email.
THANK YOU! that worked. should've thought of that. i actually saw it on another unrelated question and thought that wont be it. thanks man.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.