PDA

View Full Version : getting at name=var[] form values


firepages
02-12-2003, 05:06 PM
err dont know how else to describe...

I am sure I have asked this b4 but cant remember when or what for :)

I want to be able to get at the value of a form input's named as so...

<input type="text" name="fieldname[]" value="" />
<input type="text" name="fieldname[]" value="" />
<input type="text" name="fieldname[]" value="" />
etc//

how would I reference these values in javascript ?


document.formname.fieldname.??

Danne
02-12-2003, 06:25 PM
If you mean that you want some fields on a collection, try this:



<input type="text" name="fieldname" value="">
<input type="text" name="fieldname" value="">
<input type="text" name="fieldname" value="">




When more than one element has the same name, it's turned into a collection...

If I'm not much mistaking, VB works the same way if anyone should have the slightest interest of that...:D

beetle
02-12-2003, 06:36 PM
Danne is right. They are a collection (array)

var fields = document.forms[0].elements['fieldname[]'];

firepages
02-14-2003, 07:57 AM
sorry for the delay getting back !

OK I cant do exactly as Danne suggests as I need $fieldname as an array on the receiving page and that way I only get 1 value passed..

complicating thing slightly more :) ... because the form is dynamic I will not know form sure which no element the field I want is, so I would like to access it by name if possible..

with that in mind, is it possible (and is so a simple example would be lovely!) to get and set these form values...

<form name="bella" meth.... etc
<input type="text" name="field1[]" value="" />
<input type="text" name="field1[]" value="" />
<input type="text" name="field1[]" value="" />

where I need to get field1[1] & set field1[2] (the third is readonly so it does not matter)

any pointers appreciated !

beetle
02-14-2003, 08:47 AM
I thought I had shown that?

var fields = document.bella.elements['field1[]'];
fields[0].value = 'someValue';
fields[1].value = 'someOtherValue';

firepages
02-14-2003, 09:27 AM
I thought I had shown that?

well when you put it like that ... you have !

you are assuming I stopped and thought about it :)

cheers !

whammy
02-15-2003, 01:43 AM
LOL... don't worry firepages. You should see some of my newbie questions in .NET. ;)

firepages
02-15-2003, 02:06 AM
lol - & it aint getting any better here either ;)

I still cant set the value.. I can get it which is 90% of the job but I still need to set a value if for instance a user enters a string instead of a number... i.e. below...


<?
var tote_fields =new Array('d_cartage','d_unpacking','d_wait_time',
'd_fork_hire','d_storage','d_redirect','d_ex_1','d_ex_2','d_ex_3');

function update_tote(clicked_field){
var len=8;//tote_fields.size;.length ??//
document.bella.elements['d_tote'].value=0;
var total=0;
for(x=0;x<=len;x++){
var fieldname=tote_fields[x];
var fields = document.bella.elements[fieldname+'[]'];
total = total + fields[0].value * 1;
}
document.bella.d_tote.value=total;

}

function check_int(varin,what){
var intValue=parseInt(varin);
if(varin){
if (isNaN(intValue)) {
var fields = document.bella.elements[what+'[]'];
//HERE//
alert(varin+" is not a number"+what);
}else{
update_tote(what);
}
}
}
?>


so check_int is called as so..
onblur="check_int(this.value,this.name)";

which if is a number gets passed to update_tote() function
but if it is not I need to reset the erroroneous entry to 0 or blank and above where I comment //HERE// I have tried..

fields[0].value=0;
but that gives an error

any ideas how I may set that ? -

firepages
02-15-2003, 02:52 AM
..... thanks to JKD who got me using

onChange="check_int(this);

[PHP]
<?
function check_int(input){
var intValue=parseInt(input.value);
if(input.value){
if (isNaN(intValue)) {
input.value=0;
alert(this.value+" is not a number");
}else{
update_tote(input.name);
}
}
}
?>
[PHP]

which does the job , thanks all for the help all