View Full Version : PHP POST arrays/Javascript validations

May 14, 2004, 12:17 PM
i'm loading some post variables into a php array by combining them as such:<form name="myform" action="action.php" method="post" onSubmit="return myValidateFunction()">
<input type="checkbox" name="array[]" value="value1">
<input type="checkbox" name="array[]" value="value2">
<input type="checkbox" name="array[]" value="value3">
<input type="checkbox" name="array[]" value="value4">
<input type="submit" value="Go">

but i can't figure out how to validate that a box has been checked via javascript. at least, i can't reference those checkboxes by name, ie by document.myform.array or document.myform.array[], due to either the brackets or just the multiple elements with the same name.

is there a way to reference those boxes without calling them by name? like, forms[0].elements[somethingsomething] etc? i'm unclear on the DOM and how it describes forms.


May 14, 2004, 04:10 PM
and the answer is: assign each input a unique ID and use getElementById. as a bonus, i was generating these inputs dynamically with PHP, so i can use the same code to write the JavaScript. which means the validation function will find these inputs even if i move them around the form, which would not be the case if i used the static form.element[x]. behold:

$array[] = "Thing one";
$array[] = "Thing two";
$array[] = "Thing three";
$array[] = "Thing four";
$array[] = "Thing five";
$array[] = "Thing six";

function validateThis()
for(i = 1; i < <?=sizeof($array)?>; i++)
var thisCheckBox = 'box' + i;
if (document.getElementById(thisCheckBox).checked)
return true;
return false;

<form name="theForm" action="<?=$PHP_SELF?>" method="post" onSubmit="return validateThis()">
for($i = 0; $i < sizeof($array); $i++)
echo '<input type="checkbox" name="theArray[]" id="box'.$i.'" value="'.$array["$i"].'">'.$array["$i"]."\n";
<input type="submit" value="Submit"/>

so i end up with a bunch of checkboxes with IDs of "box1", "box2", and so on. if none of them are checked, the submit fails. if one or more are checked, the form returns an array called "theArray" with elements corresponding to the values of the checked boxes. the best part is, all i have to do to change the number of checkboxes or their values is to edit the original array. sometimes it's almost too easy. :D

Sep 8, 2006, 08:40 AM
I think I can save you alot more time....

Do it exactly as you wanted to begin with.
Lets create a simple Form....

<form name="myform" action="post">
Check/Uncheck All : <input type="checkbox" name="listmaster" id="listmaster" value="1" onclick="checkBoxes(this,"myform","mylist")" /><br />

<input type="checkbox" name="mylist[]" value="1" /><br />
<input type="checkbox" name="mylist[]" value="2" /><br />
<input type="checkbox" name="mylist[]" value="3" /><br />
<input type="checkbox" name="mylist[]" value="4" /><br />
<input type="submit" value="Continue" />
Then to check and uncheck the input fields above you use a simple bit of javascript. (Place it in the Head)

<script type="text/javascript">
function checkBoxes(obj,formname,fieldname){
for (i = 0; i < document.forms[formname].elements[fieldname+"[]"].length; i++){
document.forms[formname].elements[fieldname+"[]"][i].checked = obj.checked;


You basically pass 3 parameters into the listmaster checkbox... It's so you can re-used the function for any list you want on the same page.

1). this - being itself
2). The formname the inputs are all in
3). The global name of the checkboxes you want to check/uncheck

No PHP involved.


Then when you submit the form you simple read the $_POST array.

foreach($_POST["mylist"] as $foo){
print $foo."<br />";

It will only contain values that are ticked. In most cases this is probably an id number.

Sorted... (bows) :D

p.s - apologies if there are typos, im in a rush but wanted to help anyone else coming across this. Its not a common problem to find :)

Sep 9, 2006, 04:02 AM
Wouldn't it be better just to use $_REQUIRED?

Sep 10, 2006, 12:46 PM
Use "$_REQUIRED" ??

Bravo.. that's solves the answer to everything.... scrap knowing PHP and Javascript everyone... the answer is $_REQUIRED... sorted.


Sep 25, 2006, 09:43 AM
I've just noticed a problem with the code.
If you only have one item in the list, there isn't a length for the input array[].
You'd think it would be one but I think what's happening is, because it's only one, it's not noticing it as an array so it has no length.

Anyway... just use this instead. I'm sure there's a shorter way to do it but this works....

for (i = 0; i < document.forms[formname].elements[fieldname+"[]"].length; i++){
document.forms[formname].elements[fieldname+"[]"][i].checked = obj.checked;
document.forms[formname].elements[fieldname+"[]"].checked = obj.checked;