|
|
| Welcome to the Mac Forums forums. Please read the FAQ if you have questions. Register to participate. |
|
|||||||
| TouchArcade.com - iPhone Game Reviews and News |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
macrumors 6502a
Join Date: Jun 2003
|
PHP POST arrays/Javascript validations
i'm loading some post variables into a php array by combining them as such:
Code:
<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> 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... |
|
|
|
|
|
#2 |
|
Thread Starter
macrumors 6502a
Join Date: Jun 2003
|
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 Code:
|
|
|
|
|
|
#3 |
|
macrumors newbie
Join Date: Sep 2006
|
Actually, the answer is.......
I think I can save you alot more time....
Do it exactly as you wanted to begin with. Lets create a simple Form.... Code:
<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> Code:
<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>
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 PHP Code:
Sorted... (bows) 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
|
|
|
|
|
|
#4 |
|
macrumors regular
|
Wouldn't it be better just to use $_REQUIRED?
__________________
Come stay in my french chateau, www.chateaudelucy.com Visit my web design site www.getonthenet.eu |
|
|
|
|
|
#5 |
|
macrumors newbie
Join Date: Sep 2006
|
What does that mean?
Use "$_REQUIRED" ??
Bravo.. that's solves the answer to everything.... scrap knowing PHP and Javascript everyone... the answer is $_REQUIRED... sorted.
|
|
|
|
|
|
#6 |
|
macrumors newbie
Join Date: Sep 2006
|
Correction to Javascript Code
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.... Code:
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;
}
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|