Click to See Complete Forum and Search --> : Array problems


mixedcream
04-17-2003, 06:05 PM
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=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 :

http://www.eng.nene.ac.uk/~bf01iamc/php/pcmega/first.php

MixedCream

khalidali63
04-17-2003, 06:09 PM
What is it your code suppose to do....no details...a briefe description be good...we don't want you type that long message again .do we?

:D

mixedcream
04-17-2003, 06:34 PM
Also....I thought the array wasn't being built but the function is being called as I've tested with some document.write("") calls at either end of it.

I thought possible problems could be, the way I'm coding the array declaration i.e. the number of elements is not being stated, semi-colons, etc...

Also the fact that several elements in the array are empty could be an issue.


Oh, and when I assign the value from the array to the price text field, should I use a different way i.e. :

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

Thanks again.

MixedCream

DrDaMour
04-17-2003, 07:14 PM
first off, the items array is in scope of your function only, so once that function ends, the items array is no longer in scope. What i did to make that work just remove the function declaration. adn clsoing } that makes it a global variable.

mixedcream
04-17-2003, 07:26 PM
Thanks I think that works but I am having another error. I thought I had already tried removing the function tags but obviously not.

The error now is

Items[...].price is null or not an object

Is this to do with the other thing I mentioned and the way I am trying to assign the value to the text field.

Also, as another point, have you got any idea why the line number it gives is nowhere near the source of the error. Is it to do with the comments or something?

Thanks again

MixedCream

DrDaMour
04-17-2003, 07:29 PM
second wouldn't you know it id's with undertows are not allowed, doesn't that SUCK.

third when you refer to aform by id you have to do

document.all.FORMNAME....

your forgot that all thing.

what i would suggest:

all yoru functions for changed shoudl take a paramater that is the object being changed.

<select id="processor" onchange="processor_change(this)">



function processor_change(it){
it.options[it.selectedIndex].value
etc.

}


see in that place the varible it = document.all.build.processor
isn;'t that nice?

plus since netscape needs document.layers instead of all, you avoid that fiasco.

i changed the processor to look like this

function change_processor(){

document.all.build.processorprice.value = Items[document.all.build.processorselect.options[document.all.build.processorselect.selectedIndex].value].price;
document.all.build.processorimage.src = Items[document.all.build.processorselect.value].image;

}


and it worked

mixedcream
04-18-2003, 06:16 AM
Hi,

Thanks for all your help. I kept it the same but took the variable out of the function to make it global and tok the underscores out of the field names.

I'm a bit confused because I made the same page before for an earlier assignment and the processor_select was fine then.

So not sure why it won't work now, because the only difference is I'm using PHP to build the page and the array.

But hey, its working :)

Thanks again

Mixedcream

PS. I'm SURE I'll be back lol.