[PHP] validating form data via javaScript when stored in an array
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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] validating form data via javaScript when stored in an array
Joseph, You will have to declare SCRIPT LANGUAGE="javascript 1.2" myArray = new Array(); myArray[0] = "a string"; myArray[1] = 11; myArray[2] = new Array(); /SCRIPT since JavaScript 1.2 has arrays implemented. Kind regards, Morten Winkler -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] validating form data via javaScript when stored in an array
Morten Winkler Jrgensen wrote: You will have to declare SCRIPT LANGUAGE="javascript 1.2" myArray = new Array(); myArray[0] = "a string"; myArray[1] = 11; myArray[2] = new Array(); /SCRIPT since JavaScript 1.2 has arrays implemented. Thanks for that, but my problem being that I have just collected all my form data in an array: input[keyname] how do I now build the equivelent js array to validate the data? I tried the following still does not work: --snip from head-- script language="JavaScript1.2" //-- input = new Array(); 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-- How do I force all the data into the javascript array, while still setting the array to input[keyname]? Any help appreciated, Regards Joseph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] validating form data via javaScript when stored in an array
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;in;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" [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]