Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Array of formfields, acces element of

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Array of formfields, acces element of

Postby DutchBen » Thu Oct 24, 2002 10:32 am

Does anybody know where i'm making a mistake?

I'm using a dynamicaly generated form, depending on user input its asking for personal information from 1 up to 9 people. As a backend scripting language i'm using php.

Im using a JavaScript to validate the form, this script looks like this:

function verify(f){
var msg;
var empty_fields = "";

for (var i = 0; i < f.length; i++){
var e = f.elements[i];

if(e.mandatory){
if((e.value == null) || (e.value == ""){
empty_fields += "\n- " + e.name;
continue;
}
}
}
To call this function -
<form onsumbit="lastname.madatory = true;verify(this)">
(Feel free to use .. its from the O'Reilly Rhino Book)

For 9 people i'm using the field lastname 9 different times, each time with the exact same name. Like so
--
Person 1 <input type="tekst" name="lastname">
Person 2 <input type="tekst" name="lastname">
--
This is actually no problem, the 9 different instances are stored in an array and all of them are traversed and checked seperately.

Now for my problem:
When the form is sent Php overwrites the first occurence of lastname with a later one. I end up with only one lastname where i need more.
The solution to this problem is to name the fields lastname[]. Php now automatically creates an array by the name of lastname containing all lastnames when the form is sent.
But when i do this the validation script doesn't work anymore, it might have something to do with the brackets but i'm not sure. I'm stuck between a rock and a hard place as the song goes.
Does anybody know a solution to this problem, preferably in JavaScript, it would be a real hassle to fix it in Php.

Thanks for even reading the whole thing :)

Ruben
DutchBen
New php-forum User
New php-forum User
 
Posts: 36
Joined: Thu Oct 24, 2002 10:28 am
Location: Amsterdam

Postby Jay » Thu Oct 24, 2002 1:08 pm

The way to do it is to use the name as an array (ie name='lastname[]'), but you have to get your JS to check it OK. You can use the name as a variable in JS (for instance var field = "lastname[]") and then use an eval statement to check it. I know it's complicated but it can be done because I do it, even though I was told not to ;)

Here's a little snippet of some code I use, try to figure out what's going on and adapt it:

artists = 0;
n = "";
selectName = "artistID[]";
eval ("artistQty = document.form['" + selectName + "'].length;");
for(i=0;i<=(artistQty-1);i++) {
check = "if (document.form['" + selectName + "'].options[" + i + "].selected) ";
doArtist = "artists++;";
doName = "n += document.form['" + selectName + "'].options[" + i + "].text + \"\\n\";";
eval (check + "{ " + doArtist + doName + "}");
}
Jay
 

Thanks :)

Postby DutchBen » Fri Oct 25, 2002 10:02 am

Thanks for this anserw, i've got my code working now .. both Php and JS :D The solution i used is somewhat different tho. The problem was in a different part of my script .. i was looking at the wrong thing. I actually use a function to initialize the fields i want to be mandatory
Code: Select all
//Bad
function checkit(c){
   for(var I = 0;n < c.Achternaam[].length;n++){
    c.Achternaam[][n].verplicht = true;
  }
   var checker = verify(c);
   return checker;
}


With Achternaam being dutch fot lastname and verplicht being dutch for mandatory. And c being a form object (document.the_form)
This wasnt working because of the double brackets. I changed it to

Code: Select all
function checkit(c){

var d = c.elements["Achternaam[]"];

for(var n = 0;n < d.length;n++){
     c.elements["Achternaam[]"][n].verplicht = true;
}
     var checker = verify(c);
     return checker;
}

Thx to Dave Clark, JS forum

The function verify (see above) can remain the same now.
Its working really well now ... even on NS4. It needs some more tweaking to have it work even if there's only one person to check.

My question wasnt the right one. Didn't mean to turn this in to a JS forum.

Thanx for your trouble tho.
DutchBen
New php-forum User
New php-forum User
 
Posts: 36
Joined: Thu Oct 24, 2002 10:28 am
Location: Amsterdam


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron