views:

55

answers:

1

Hello, I am trying to make a page that allows users to select 8 checkboxes from a total of 25.

Im wondering, how to detect the exact order in which they check them. I am using a plain html front page that will be verified by a form action pointing to a php page.

Im trying to get a result like (checkbox1,checkbox2,checkbox6,checkbox3,checkbox7,etc) for eight checkboxes, and the exact order in which they were clicked.

I think I have found what I am looking for,Im not too sure, but Im having trouble implementing it.

This is what I have so far, I guess my question is, what type of php do I need to gather this info once a user has submitted the form.

For the form I have:

<form id="form1" name="form1" method="post" action="check_combination.php">
    <label id="lblA1"></label> 
    <input name="checkbox1" type="checkbox" value="a1" onclick="setChecks(this)"/> Option 1 
    <label id="lblA2"></label> 
    <input name="checkbox1" type="checkbox" value="a2" onclick="setChecks(this)"/> Option 2 
    <label id="lblA3"></label> 
    <input name="checkbox1" type="checkbox" value="a3" onclick="setChecks(this)"/> Option 3 
    <label id="lblA4"></label> 
    <input name="checkbox1" type="checkbox" value="a4" onclick="setChecks(this)"/> Option 4
</form>

For the Javascript I have:

<script type="text/javascript">
<!--
//initial checkCount of zero
var checkCount=0

//maximum number of allowed checked boxes
var maxChecks=8

function setChecks(obj){
//increment/decrement checkCount
if(obj.checked){
checkCount=checkCount+1
}else{
checkCount=checkCount-1
}
//if they checked a 4th box, uncheck the box, then decrement checkcount and pop alert
if (checkCount>maxChecks){
obj.checked=false
checkCount=checkCount-1
alert('you may only choose up to '+maxChecks+' options')
}
}
//-->
</script>

<script type="text/javascript">
<!--
$(document).ready(function () { 
    var array = []; 
    $('input[name="checkbox1"]').click(function () { 
        if ($(this).attr('checked')) { 
            // Add the new element if checked: 
            array.push($(this).attr('value')); 
        } 
        else { 
            // Remove the element if unchecked: 
            for (var i = 0; i < array.length; i++) { 
                if (array[i] == $(this).attr('value')) { 
                    array.splice(i, 1); 
                } 
            } 
        } 
        // Clear all labels: 
        $("label").each(function (i, elem) { 
            $(elem).html(""); 
        }); 
        // Check the array and update labels. 
        for (var i = 0; i < array.length; i++) { 
            if (i == 0) { 
                $("#lbl" + array[i].toUpperCase()).html("first"); 
            } 
            if (i == 1) { 
                $("#lbl" + array[i].toUpperCase()).html("second"); 
            } 
        } 
    }); 
});
//-->
</script>

I have gotten the part that only allows 8 checkboxes to be checked, but Im stuck as to what I need to do to actually parse the data once it has been submitted to a page with a name like check_combination.php.

I would appreciate any help

A: 
  • create a hidden input field with the order
  • update this input field when something changes
  • you'll have the order ready to be processed by PHP
galambalazs
I have to admit that I am an extreme newbie. Ive created hidden fields before, but I never tried it with any array of data. How exactly would I create the input field to include an array of data, for example, checkbox1,checbox3,checkbox6,checkbox2 etc get passed on to the php script.
James
`hidden_field.value = array.join(',');` then on the php side `$order = explode(',', $_POST['hidden_field']);` and you get a nice array indexed in selection order.
galambalazs
Any chance you can help me implement that in the above code? Im a little confused. Ive gotten it to parse on the php page since I posted this question, but not in the correct order, and from the look of your code, I am way off I was trying something like: <? $checkbox = $_POST["checkbox"]; while (list ($key,$val) = @each ($checkbox)) { echo "$val,"; } ?> on the php page, but like I said, i see that Im way off.just to clarify things, I sourced the code in my question from various sources, im kind of a noob, I can almost paste stuff together and get it working, but Im not quite there yet.
James
i don't get what your problem is. :) Your `array` in your javascript code seems to be ordered. You just need to pass it to PHP in a hidden field then parse it into an array with `explode`.
galambalazs
My problem isnt the code, its operator error, my problem resides between the back of my chair and my keyboard :/. Im gonna be honest, I have no clue were to stick the code you provided above. but Im learning more and more as I go, trying to at least lol
James
When you use explode, you don't need list. So, you have the order in your hidden form field, which is sent as a post var. ON the PHP side you do $order-clicked = explode(',', $_POST['hidden']). This splits it at the commas and creates an array, so $order-clicked[0] will be the first one clicked. foreach ($order-clicked as $key => $val) whill let you go through them.
Liam Bailey
Im starting to understand, but what im not getting is where to add the hidden field in the javascript, I have jquery installed and working. Just not sure where to add the hidden field.
James
you can simply add it to your HTML: `<input name="hidden_field" type="hidden" />`
galambalazs
oh, I thought this: hidden_field.value = array.join(','); looked like something to add to the javascript.How do I grab the data from the javascript as a hidden field in order to post it to the php page in the correct order.Im not understanding how to get the hidden field to read what the javascript is making an array of, hope that makes sense
James
Somthing like this?'<input name="checkbox1" type="hidden" />'and on the php:'$order-clicked = explode(',', $_POST['checkbox1'])'
James
I apologize for not understanding, I made another question that was a little more specific, hopefully someone can explain what I would need to add to get this running. Thanks and best Regards,
James