Re: validating form data via javaScript when stored in an array

From: Date: Mon, 09 Apr 2001 19:10:18 +0000
Subject: Re: validating form data via javaScript when stored in an array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-47747@lists.php.net to get a copy of this message
I don't think you can name variables as varname[name] and access them like that in javascript unless varname[name] is initialize where varname is an array and name=index in that array and it must be defined first. I believe you will have to change the naming convention from var[keyname] to var_keyname or something, and same for reading it from PHP. or else name them as: input_1 input_2 input_... input_N then you can access them in JavaScript as: for (i=0;i<n;i++) { fld=eval("form.input_"+i); if (fld.value=="") { alert('error in field'); fld.focus(); } } as for PHP and reading them, you have to do: <? for ($i=0;$i<$n;$i++) { $temp = "input_$i"; $value = $$temp; echo "value $i=$value"; } ?> hope it helps. -elias http://www.kameelah.org/eassoft "Joseph Blythe" <joe.blythe@binarylogic.com.au> wrote in message news:3AD163C2.5060107@binarylogic.com.au... > G'day, > > I was wondering if anyone knows how to get the following to work: > > --snip from head-- > <script language="JavaScript"> > //--> > function validateForm(theForm) { > if ( theForm.input[name].value == "" ) { > alert("You must type a value for NAME."); > theForm.input[name].focus(); > return false; > } > > } > <!-- > </script> > > --/snip-- > --snip from body--- > > <form name="form" method="post" action="whatever" > onSubmit="return > validateForm(this);"> > <input type="text" name="input[name]"> > <input type="text" name="input[address]"> > <input type="submit" value="submit"> > </form> > > --/snip-- > > Now the above doesn't work as I think javascript does not understand > arrays? or I don't understand how javascript deals with arrays?? > > Any help/suggestions would be great, sorry if this post is a little off > topic in the way of javascript but I would not be having this trouble if > I weren't passing the form data to php via an array :-) > > Regards, > > Joseph. > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#47747) next »