Page 1 of 1

[solved]html forms array and javascript array

Posted: Thu Feb 09, 2006 1:27 pm
by mushi
I have a simple form that generated another set of forms depending on how customers are entered.

First form: how many customer?

Second file: If the answer is 2, it generated 2 forms with exactly the same fields. It's stored in an array.
My question is how to have javascript read the values in that array. Thanks!!

Here's a sample of the 2nd file.

Code: Select all

echo "<form name='totalcomp' action='average2.php' method='POST'>";
	for ($i=0; $i<$number; $i++)
	{
?>

	
	<fieldset style="width:300px; height:150px"><legend>Compute Average</legend>
	<div id = "box2">
	<table border = "0">
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td>Number 1:</td>
			<td><input type="text" name="num1[]" /></td>
		</tr>
		<tr>
			<td>Number 2:</td>
			<td><input type="text" name="num2[]" /></td>
		</tr>
		<tr>
			<td>Total:</td>
			<td><input type="text" name="total[]" onBlur = "mytest()"/></td>
		</tr>
		
		

	</table>
	</div>
	</fieldset>
	<?}?>
	<tr>
		<td><input type="submit" name = "submit" value="submit" /></td>
		</tr>
	</form>

Re: html forms array and javascript array

Posted: Thu Feb 09, 2006 2:02 pm
by Weirdan
mushi wrote: Second file: If the answer is 2, it generated 2 forms with exactly the same fields. It's stored in an array.
My question is how to have javascript read the values in that array. Thanks!!
Either give your fields distinct ids ('item1', 'item2' ...) and use document.getElementById or use document.getElementsByName

Posted: Tue Feb 14, 2006 3:47 pm
by mushi
Thank you for replying. I got it to works in IE, but it doesn't work in Firefox.
This is the error in firefox:

Error: document.getElementsByName("num1")[j] has no properties.

Is there a reason why it would work in one browser and not the other?