PDA

View Full Version : Checkboxes and arrays



gilbertsavier
04 Aug 2009, 04:42 AM
Hi,
I need some code which can give an array some values, depending on which checkboxes which is selected.
I think I will give these checkboxes (lets say 10 checkboxes) some values, and when you have selected the relevant checkboxes, you will have to click a button: "Create array". Then a array is created, and it will have the same number of values as there are checkboxes selected. And the values in the array will be the values of the selected checkboxes.

If I, for instance, select checkbox number 1, 4 and 7, and I click "Create array", the content of the array will be: 1,4,7

What will the code be for that. I donīt have the javascript code, only the HTML:

HTML Code:

<input type="checkbox" name="alotofcheckboxes" value="1" />
<input type="checkbox" name="alotofcheckboxes" value="2" />
<input type="checkbox" name="alotofcheckboxes" value="3" />
<input type="checkbox" name="alotofcheckboxes" value="4" />
<input type="checkbox" name="alotofcheckboxes" value="5" />
<input type="checkbox" name="alotofcheckboxes" value="6" />
<input type="checkbox" name="alotofcheckboxes" value="7" />
<input type="checkbox" name="alotofcheckboxes" value="8" />
<input type="checkbox" name="alotofcheckboxes" value="9" />
<input type="checkbox" name="alotofcheckboxes" value="10" />

<br><br>

<input type="button" value="Create array" onclick=???????????

------------------------------------
Thanks & regards
Lokananth
Live Chat Software (http://www.mioot.com) By miOOt

djlebarron
04 Aug 2009, 11:42 AM
I believe that to create an array, you must use brackets following the name.


<form name="yourform" action="formresults.php" method="post">

<input type="checkbox" name="alotofcheckboxes[]" value="1" />
<input type="checkbox" name="alotofcheckboxes[]" value="2" />
<input type="checkbox" name="alotofcheckboxes[]" value="3" />
<input type="checkbox" name="alotofcheckboxes[]" value="4" />
<input type="checkbox" name="alotofcheckboxes[]" value="5" />
<input type="checkbox" name="alotofcheckboxes[]" value="6" />
<input type="checkbox" name="alotofcheckboxes[]" value="7" />
<input type="checkbox" name="alotofcheckboxes[]" value="8" />
<input type="checkbox" name="alotofcheckboxes[]" value="9" />
<input type="checkbox" name="alotofcheckboxes[]" value="10" />

<br><br>

<input type="button" value="Create array"

</form>

If you want to display the array on a new/different page, make that different page your "action" and put the following on that page where you want the array to appear:

<?php
$array=$_POST["alotofcheckboxes"];
print_r ($array);
?>
If you want to display the results to the same page your form is on, make that same page your "action and again, put that code on that page where you want the array to appear.
If you wanted to display an output like:
Array(1, 4, 7)
You'd put this where you want it to display:

<?php
$array=$_POST["alotofcheckboxes"];
echo "Array(".$array[0];
array_shift($array);
foreach ($array as $value)
{
echo ", ".$value;
}
echo ")"; ?>
If 1, 4, 5, 7 and 9 have been checked the output that the code would produce:
Array(1, 4, 5, 7, 9)
There are a lot of ways to manipulate the display of the values.
There's the php function count() that will give you the number of elements in your array. You could display something like: "1, 4, 5, 7 and 9" by using $last=count($array), using array_shift($array) and array_pop($array), again using a foreach ($array as $value) statement and trailing an echo "and ".$array[$last] at the end.
I'm not that far into it, but it might be possible and simpler to use a while loop instead of array_shift and array_pop and the foreach function.
This is all assuming you may want to use php instead of js. There are a lot of advantages in using php.
w3schools has some good tutorials and more importantly, a chart of php functions and constants.

Edit: probably an easier way would be to use a php while statement and i=i++ to name your array items "$item1", "$item2", etc. Then it would be easier to echo them in whatever configuration you want with whatever punctuation you want without all the shifting and popping. I'm still learning and don't have much time right now, but maybe one of the php guys here could supply the code for that.

What are you using the array for?