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!

Member Avatar for iamthwee

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 for diafol

Try this:

<?php

if($_POST){
    if(isset($_POST['hiddenarray'])){
        $array = json_decode($_POST['hiddenarray']);
        print_r($array);
    }
}

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

   <body>
   <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" />
   </form>



   <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
   <script>
        var checkarray = Array();
        $('.mycheck').click(function(){
            id = $(this).attr('id');
            if($(this).attr('checked')){
                checkarray.push(id);    
            }else{
                checkarray.splice($.inArray(id,checkarray),1);
            } 
        });
        $('#myform').submit(function() {
            $('#hiddenarray').val(JSON.stringify(checkarray));
            return true;    
        });
   </script>
   </body>
   </html>
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 for diafol

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

Member Avatar for iamthwee

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"> 
<!--

$(document).ready(function(){

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


    $("input[type=button]").click(function(){
    for (var i = 0; i < array.length; i++){  
    if (array[i] == $(this).attr('value')){  
    array.splice(i, 1);  
    }  
    }  
    alert(array.length);
    });




    $('input[type="checkbox"]').click(function(){

    if ($(this).attr('checked')){
    // Add the new element if checked:  
    array.push($(this).attr('value'));
    //alert(array2.push($(this).attr('name')));
    }

    else{ 
    // 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);
    }
    }*/
    }
        $("#result").show(function(){
        $(this).val("");
        for (var i = 0; i < array.length; i++){
    $(this).val($(this).val() + " " + array[i]+ "\n");
        }
        });

    });

}); 
//--> 
</script>

I'm binding it here:

$result=mysql_query(" select * from itinerary group by location");
    $y=mysql_affected_rows();
    for($i=0;$i<$y;$i++)
    {$row=mysql_fetch_array($result);
        $pr=$row['icode'];
        $n=$row['location'];
        $l=$row['btime'];
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>
          </table>";

Then output it here:

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

Thank you for your help!

Hello
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
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.