RE: [PHP] Forms with Field Arrays and JavaScript

From: Date: Wed, 02 May 2001 18:37:38 +0000
Subject: RE: [PHP] Forms with Field Arrays and JavaScript
Groups: php.general 
Request: Send a blank email to php-general+get-51183@lists.php.net to get a copy of this message
For IE 5 and NN 4.7 at least, I made it work like this: for(var i = 0;i<numrows;i++){ //alert(i); //alert(theform["changed["+i+"]"].value); if (theform["changed["+i+"]"].value == 'true') { //record is changed //alert ("FOUND CHANGES"); var FREQ = theform["FREQ["+i+"]"]; var YEAR = theform["START_YEAR["+i+"]"]; var MONTH = theform["MONTH["+i+"]"]; var LBS = theform["NO_STOCKED["+i+"]"]; var NUM = theform["LBS_STOCKED["+i+"]"]; if (theform["RATE["+i+"]"]) { var RATE = theform["RATE["+i+"]"]; var str_RATE = theform["RATE["+i+"]"].value; } var str_qual = theform["QUAL["+i+"]"].value; if(isNaN(LBS.value)) { return warnInvalid (LBS, "LBS is not a number"); } if (isNaN(NUM.value)) { return warnInvalid (NUM, "NUM is not a number"); } return checkMonth(MONTH,false); return checkYear(YEAR,false); } //end if changed } when my form fields were named like this: <INPUT TYPE = TEXT NAME = 'RATE[<?PHP echo $row?>]'> etc. It works well >>> "Diego Fulgueira" <dful@metahogar.com> 05/02/01 09:14AM >>> 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 (#51183) next »