I have an array of checkbox that is populated with data from mysql database through a query. I can submit the value of the checked checkbox into a table from a database. But I want it to be saved according to the sequence the checkbox was checked by the user. How can I achieve this? Any help will be appreciated. Thanks a lot!

What happens if the user unchecks the box does that change the sequence?

This might be tricky. And the only way I can see doing this is to use some client side javascript processing.

@iamthwee, yes, it'll change the sequence. I am not that familiar with javascript. Can you guide me on how to do this? Thanks.

Member Avatar

Try this:


        $array = json_decode($_POST['hiddenarray']);

<!DOCTYPE html>
    <meta charset="utf-8">

   <form name="myform" id="myform" method="post">
   <input type="checkbox" id="firstcheck" class="mycheck" />
   <input type="checkbox" id="secondcheck" class="mycheck" />
   <input type="checkbox" id="thirdcheck" class="mycheck" />
   <input type="checkbox" id="fourthcheck" class="mycheck" />
   <input type="hidden" id="hiddenarray" name="hiddenarray" />
   <input type="submit" name="submit" value="submit" />

   <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
        var checkarray = Array();
            id = $(this).attr('id');
        $('#myform').submit(function() {
            return true;    
commented: Nice! +14

@diafol, that did great! Thanks! But as I said, my checkbox array is populated with data from mysql table. So that means, I only have a single array checkbox with me that is inside a loop. How can I apply this code to that?

Member Avatar

It doesn't matter where you check boxes come from they need a unique id for labels, right? Something like this:

AT top of page get checkboxes output from DB loop into string variable e.g. $checks
Replace the html checkboxes with echo $checks;

SHould be straightforward

Yeah, just dynamically assign the html with PHP to your checkboxes.

If you need help just say and show us what your PHP code is for getting the data from your db.

Got it working with javascript code.

<script type="text/javascript"> 


    var array = [];
     //var array2 = [];

    for (var i = 0; i < array.length; i++){  
    if (array[i] == $(this).attr('value')){  
    array.splice(i, 1);  


    if ($(this).attr('checked')){
    // Add the new element if checked:  

    // Remove the element if unchecked:  
    for (var i = 0; i < array.length; i++){  
    if (array[i] == $(this).attr('value')){  
    array.splice(i, 1);
    /* if (array2[i] == $(this).attr('name')){  
    array2.splice(i, 1);
        for (var i = 0; i < array.length; i++){
    $(this).val($(this).val() + " " + array[i]+ "\n");



I'm binding it here:

$result=mysql_query(" select * from itinerary group by location");
echo"<table border=1 align=center width=250>
          <tr><td width=15>
          <input type='checkbox' value='$n -> $l' onClick='setChecks();' id='cb1'>
</td><td>$n</td><td width=4>$l</td></tr>

Then output it here:

<textarea name=resulta id='result' style='height:200px; width:350px' disabled='disabled'></textarea>

Thank you for your help!

i have a similar problem but ... reading the solutions above i am guessing it might solve my problem.but just that .... what if i have sound files in my array !

i hav to develop a simple table of 5*5 ...each cell/ checkbox, storing diffenrt sound files...
i want the user to check the checkboxes randomly and there should be function which records the sequence in which the user has checked them... and then after pressing a playbutton ....soundfiles are played one at a time... one sound finishes and immeditly another comes... basically concatenate...and then thy should keep loopng in same order eeks ...

i hav zero knwldge of sql and php...i am currtnly wrkng with html5 and js only...
by far i hav the checkboxes ready and they play when thy are selcted.and all the sounds are coming as thy are pressed ..i could pass u the code if u could try n help me and send me email id..

it would b a great help