RE: [PHP] Forms with Field Arrays and JavaScript

From: Date: Wed, 02 May 2001 14:14:12 +0000
Subject: RE: [PHP] Forms with Field Arrays and JavaScript
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-51159@lists.php.net to get a copy of this message
After several tries, I am convinced there is no way to do it. I went around the problem by using dinamic field names (var1, var2, var3, etc) and then reading them using $HTTP_POST_VARS["var".$i], where $i would be your former index to the array. For example: The form coud have HTML code like this: <input type=text name=myvar0> <input type=text name=myvar1> <input type=text name=myvar2> Each field can be referenced by JavaScript code like this: document.myform.myvar1.value="hello"; Or dynamically like this: myfield=new array(); for(i=0;;i++) if(!eval("document.myform.myvar"+i)) break; myfield[i]=eval("document.myform.myvar"+i); } myfield[1].value="hello"; When the form is submitted, asuming, method=POST, then you get the field values like this: $myvars=array(); for($i=0;;$i++) { if(empty($HTTP_POST_VARS["myvar".$i]) break; $myvars[$i]=$HTTP_POST_VARS["myvar".$i]; } Hope it helps. Cheers, Diego. -----Original Message----- From: Utopia [mailto:admin@callutopia.com] Sent: Tuesday, 01 May, 2001 10:32 PM To: dful@metahogar.com Subject: Re: [PHP] Forms with Field Arrays and JavaScript Hello, I see you sent this back in January. I'm having the very same problem. Could you PLLLLEEEAAASSEEE tell me how to assign a value to the 'myfields[]' text field using JavaScript. I can't make it happen. Thanks. Tiffany ---------------------------- THIS WAS YOUR MESSAGE ON THE BOARD Hi! I was wondering if someone knows, or can think of a way to go around this problem i have. The thing is i am working with a form with a field array. Each field is something like: <input name="myfields[]" type="text"> When i receive the posted data, I can access the "myfields" array. This is fine. The problem is within my form i have client JavaScript code to validate the form data. But I can't make reference to a form object called myfields[] or myfields[5] or just myfields. The property name of the javascript object is set to "myfields[]", but reference to myfields[][5] or document.myform.fields["myfields[]"] doesn't work either. Any suggestions will be greatly appreciated. Thanks in advance.

« previous php.general (#51159) next »