Register FAQ/Rules Forum Spy Search Today's Posts Mark Forums Read

Welcome to the Mac Forums forums. Please read the FAQ if you have questions. Register to participate.

 
Go Back   Mac Forums > Special Interests > Web Design and Development
TouchArcade.com - iPhone Game Reviews and News

Reply
 
Thread Tools Search this Thread Display Modes
Old May 14, 2004, 01:17 PM   #1
sonofslim
macrumors 6502a
 
sonofslim's Avatar
 
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>
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 is offline   Reply With Quote
Old May 14, 2004, 05:10 PM   #2
sonofslim
Thread Starter
macrumors 6502a
 
sonofslim's Avatar
 
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:
<?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.
sonofslim is offline   Reply With Quote
Old Sep 8, 2006, 09:40 AM   #3
adaminco
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>
Then to check and uncheck the input fields above you use a simple bit of javascript. (Place it in the Head)

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

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

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
adaminco is offline   Reply With Quote
Old Sep 9, 2006, 05:02 AM   #4
andy89
macrumors regular
 
Join Date: May 2005
Location: Chateau de Lucy, France
Send a message via MSN to andy89 Send a message via Skype™ to andy89
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
andy89 is offline   Reply With Quote
Old Sep 10, 2006, 01:46 PM   #5
adaminco
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.

adaminco is offline   Reply With Quote
Old Sep 25, 2006, 10:43 AM   #6
adaminco
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;
}
adaminco is offline   Reply With Quote

Reply

Mac Forums > Special Interests > Web Design and Development

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 05:24 AM.

Mac News | Mac Rumors | iPhone Game Reviews | iPhone Apps

Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright 2002-2009, MacRumors.com, LLC