Re: Couple of questions
| From: | Tom Rogers | Date: | Thu, 24 Oct 2002 18:21:19 +0000 |
| Subject: | Re: Couple of questions | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-121330@lists.php.net to get a copy of this message | ||
Hi,
Friday, October 25, 2002, 3:46:04 AM, you wrote:
JN> Hi guys and gals,
JN> I know this list is for php, but I've been trying to find an answer to
JN> a JavaScript releated question for the past two day, and there has been
JN> no response on the two JS mailing lists I belong too. I have two
JN> questions....
JN> 1) Does anyone know of a JS mailing list that actually has a good
JN> amount of traffic (receives more than 10 email messages a day)?
JN> 2) I'm trying to validate a checkbox element in a form. Due to using
JN> php to process this form, the checkbox element must have a name ending
JN> in square brackets...
JN> <input type="checkbox" value="somevalue" name="somename[]"
JN> />
JN> The problem I'm having is trying to loop thru this element to see if at
JN> least one of the boxes are checked....
JN> for ( i = 0; i < document.forms[0].elements['somename[]'].length; i++ )
JN> {
JN> alert ( document.forms[0].elements['somename[]'][i].checked );
JN> }
JN> This gives me an error. What's the proper syntax for this?
Not sure but I think java won't accept [] in a variable name. You can
get the same effect by using name_1 name_2 and split them into an
array yourself. Not as nice as PHP doing it but it will pass java ok.
Here is a quick example:
<?
if(isset($_POST['submit'])){
$chk = array();
while(list($key,$val) = each($_POST)){
if(ereg('chk_',$key)){
$a = strtok($key,'_');
$i = strtok('_');
$chk[$i] = $val;
}
}
echo '<pre>';
print_r($chk);
echo '</pre>';
}
?>
<form action="<?echo $_SERVER['PHP_SELF']?>"
method="post">
<input type="checkbox" name="chk_1"> 1 <br>
<input type="checkbox" name="chk_2"> 2 <br>
<input type="checkbox" name="chk_3"> 3 <br>
<input type="checkbox" name="chk_4"> 4 <br>
<input type="submit" name="submit" value="submit">
</form>
--
regards,
Tom