Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have location name and location Id in database table. Using foreach loop i'm printing the values in checkbox in PHP. I have a submit button which triggers a javascript. I want the user selected all checkbox values separated by comma, in a javascript variable. How can I do this?

    <!-- Javascript -->
<script>
        function getLoc(){
                var all_location_id = document.getElementByName("location[]").value;
                     var str = <!-- Here I want the selected checkbox values, eg: 1, 4, 6 -->

            }
        <script>

foreach($cityrows as $cityrow){
echo '<input type="checkbox" name="location[]" value="'.$cityrow['location_id'].'" />'.$cityrow['location'];
echo '<br>';
}
echo '<input name="searchDonor" type="button" class="button" value="Search Donor" onclick="getLoc()" />';
share|improve this question
add comment

3 Answers

up vote 2 down vote accepted
var checkboxes = document.getElementsByName('location');
var vals = "";
for (var i=0, n=checkboxes.length;i<n;i++) {
  if (checkboxes[i].checked) 
  {
  vals += ","+checkboxes[i].value;
  }
}
if (val) val = val.substring(1);
share|improve this answer
 
Works perfectly with some perfection in above code.. by changing 'location' to 'location[]' and val to vals at the last line. :-) –  Alien2828 Nov 19 '13 at 10:26
add comment

I prefer jQuery over javascript. So, i have mentioned solution in jQuery.

function getLoc(){

    // Step-1
    // get all the checkboxes that are checked in one variable
    var boxes = $('input[name=location]:checked');

    // Step-2
    // make an array of the values of all the checkboxes
    var boxesValue = [];       // variable boxesValue initialization as array
    $(boxes).each(function(){
        // push the element into array
        boxesValue.push(value);    // remember to push value here
    });

    // Step-3
    // convert array to string using join
    var str = boxesValue.join(",");
}
share|improve this answer
 
do i need to add any jquery reference here? –  Alien2828 Nov 19 '13 at 10:28
 
@Alien2828: Yes. You have to add jquery in the page. But, i think using jQuery is a good idea in terms of up-gradation and ease. –  Bhavik Shah Nov 19 '13 at 10:36
add comment

This is a variation to get all checked checkboxes in all_location_id without using an "if" statement

var all_location_id = document.querySelectorAll('input[name="location[]"]:checked');

var aIds = [];

for(var x = 0, l = all_location_id.length; x < l;  x++)
{
    arr.push(aIds[x].value);
}

var str = aIds.join(', ');
share|improve this answer
 
Please add some explanation to your post. –  DontVoteMeDown Nov 19 '13 at 10:57
 
Ok, did it. Sorry I'm new here. Is it mandatory to add an explanation only if the answer isn't marked as "best answer"? –  ilpaijin Nov 19 '13 at 11:02
 
I wouldn't say "mandatory" but is a good pratice in order to provide a good quality question. This site cares about it's quality. I commented to you because your post was voted to be close, but it disagree with closing. Please read this. –  DontVoteMeDown Nov 19 '13 at 11:08
 
Ah Ok, I've just wonder why you just commented me and not the "best answer", maybe there was a rule which I didn't still know. Anyway, many thanks. I'll pay attention to provide more quality in my answer. –  ilpaijin Nov 19 '13 at 11:15
 
You're right. The accepted answer doesn't get the perfect pattern too. But that happened because I have been noticed about your question because somebody flagged it as "Low Quality". This make your question to be listed on a section inside the "review" section(link aside your badges on header), so I only see your post, not the entire list of answers. Feel free to flag it, or comment it if you think that is something wrong. –  DontVoteMeDown Nov 19 '13 at 11:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.