Array problems

wxdqz

New Member
Hi, I'm trying to create a web page that displays some select items that a user can choose computer parts from. When an item is chosen, the price and image have to automatically display. The details are stored in a MySQL table and I'm using PHP to build the page.

I'm sure the problem is a Javascript one so I'm just posting the JS/HTML source to here.

Here is how I've done it:

I've got a function to build a JS array with the result of an SQL query:

function build_array(){

var Items = new Array()
Items[1] = new Product(33.00, 'pictures/processors/cel12.jpg');
Items[2] = new Product(41.50, 'pictures/processors/cel17.jpg');
Items[3] = new Product(99.99, 'pictures/processors/p415.jpg');
Items[4] = new Product(99.99, 'pictures/processors/p422.jpg');
etc...etc...

}

and another:

function Product(price, image) {


this.price = price;
this.image = image;

}

The build_array function is invoked by onLoad in the body tag. The array elements are filled using the $partid field of the table like this:

Items[$partid] = new Product ($price,$image)

so some of the elements are empty including Items[0].

The select item is built in the body as follows:

<select name='processor_select' onChange='change_price();'>
option value=http://www.webdeveloper.com/forum/archive/index.php/9>Processor9</option>
etc...etc....
</select>

and the onChange function is as follows:

change_price(){

document.build.processor_price.value = Items[document.build.processor_select.value].price;

}

which gets the price of the selected item from the array and puts it into a price text field.

It is this line which is creating the error, as it is referencing the array, Items, which apparently is undefined.

Hope this is enough information and not too confusing.

Thanks in advance

Here is a link to the page in question :

<!-- m --><a class="postlink" href="http://www.eng.nene.ac.uk/~bf01iamc/php/pcmega/first.php">http://www.eng.nene.ac.uk/~bf01iamc/php ... /first.php</a><!-- m -->

MixedCream
 
Back
Top