Re: Javascript and PHP arrays

From: Date: Tue, 26 Sep 2000 18:50:31 +0000
Subject: Re: Javascript and PHP arrays
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-17683@lists.php.net to get a copy of this message
On Tue, 26 Sep 2000, David Diaz i Torrico wrote: > I'd better explain it with an example, here it comes: > > <SCRIPT> > > alert(document.form.foo[0].value); // That seems not to be > alert(document.form.bar[0][1].value); // correct, so how do > // I do it? > > </SCRIPT> > > > <FORM NAME=form> > <INPUT TYPE=TEXT NAME=foo[0] VALUE=0> > <INPUT TYPE=TEXT NAME=foo[1] VALUE=1> > <INPUT TYPE=TEXT NAME=foo[2] VALUE=2> > <INPUT TYPE=TEXT NAME=foo[4] VALUE=4> > <INPUT TYPE=TEXT NAME=foo[3] VALUE=3> > > </FORM> > Hi David, You will have to do some dirty code to make this work, but it is possible. You cannot use "foo[0]" as the name of your form element, because JavaScript doesn't work that way. JS would think of "foo[0]" as the name of the element, or the name of an object holding an array of elements, which is not your case. Anyway, you can loop thru all your form elements, and get their names from them. You can also check for specific form element types if you only need all text field elements, or anything like that. Here goes some code : <SCRIPT LANGUAGE="JavaScript"> <!-- for (var i = 0; i < document.form_name.elements.length; i++) { if (document.form_name.elements[i].type == "text") { alert("name of element = " + document.form_name.elements[i].name); alert("value of element = " + document.form_name.elements[i].value); } } //--> </SCRIPT> Regards, Joao -- Joao Prado Maia Web Developer Kefta, Inc. T: (415) 391-6881 ext 8008 F: (415) 391-7079

« previous php.general (#17683) next »