PDA

View Full Version : PHP POST arrays/Javascript validations




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

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.

thanks...



sonofslim
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:

<?php
$array[] = "Thing one";
$array[] = "Thing two";
$array[] = "Thing three";
$array[] = "Thing four";
$array[] = "Thing five";
$array[] = "Thing six";
?>

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

<html>
<form name="theForm" action="<?=$PHP_SELF?>" method="post" onSubmit="return validateThis()">
<?php
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"/>
</form>
</html>

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

adaminco
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">
<p>
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" />
</p>
</form>
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;
}
}
</script>

Done.

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.
e.g

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 :)

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

adaminco
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.

:p

adaminco
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....


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