Forum Moderators: open
I wrote a function in PHP to convert a PHP array of database results, to an array that can be used by JavaScript.
When I view the source of my page, I see that the array is created successfully and all the elements from the database and their coresponding values are included. Yet, when I try to make an alert box with the first value of the array, I get the error, "products is not defined".
I declared the products JavaScript array (without using the var keyword) by calling my PHP function within the makeJsArray() JavaScript function. I have read that not using the var keyword within a function will cause a variable to have a global scope, therefore, it should be seen by the entire script. Yet my alert box still says that products is not defined.
Any insight would be greatly appreciated.
I have included the code that appears within the
<script></script> tags in the <head></head> on my document when I view the source of my page. Thank you so much!
Rhiannon
function addLoadEvent(func)
{
var oldonLoad = window.onLoad;
if(typeof window.onLoad!= 'function')
{
window.onLoad = func;
}
else
{
window.onLoad = function()
{
oldonLoad();
func();
}
}
}
function makeJsArray()
{
products = new Array();
products[0]['part_number'] = 'CA-XMC-00';
products[0]['product_description'] = 'Shieldz for iPod mini - Ice';
products[1]['part_number'] = 'AS-TRM-100';
products[1]['product_description'] = 'The Recipe Manager';
products[2]['part_number'] = 'CA-XMC-10';
products[2]['product_description'] = 'Shieldz for iPod mini - Sky';
*** Full array length not included ***
}
addLoadEvent(makeJsArray);
alert(products[0]['part_number']);
// BAD
window.on[red]L[/red]oad;
// GOOD
window.on[blue]l[/blue]oad;
// BECAUSE
event handlers are all lower case
2)
// BAD
products = new Array();
products[0]['part_number'] = 'CA-XMC-00';
// GOOD
products = new Array();
products[0] = new Object();
products[0]['part_number'] = 'CA-XMC-00';
// BECAUSE
PS.
Is there any need to put the creation of the array inside a function in the first place?
If you leave the array in inline script it would give the JS interpreter something to do while your images are loading.
You could also save yourself a little quoting hassle by ditching the square brackets for the "associative" properties. Just as long as the names are all valid (no spaces etc).
products[0].part_number = 'CA-XMC-00';
The reason I made a function for the creation of the array is because I have to make this same array for several different applications and I wanted to be able to easily reuse it.
I wrote all of the quotes into my PHP function that converts the array because I was using a JavaScript reference for the syntax and that's what it said I should do. I didn't know you could do it without the quotes.
I don't think I have ever written JavaScript code myself. I have always hated JavaScript. I just read a fantastic book called, DOM Scripting: Web Design with JavaScript and the Document Object Model, which has really changed my mind.
Thanks again for your help :)
Rhiannon
You could go all the way...
var products =
[
{part_number:'CA-XMC-00,
product_description:'Shieldz for iPod mini - Ice'},
{part_number:'The Recipe Manager',
product_description:'AS-TRM-100'},
{part_number:'CA-XMC-10',
product_description:'Shieldz for iPod mini - Sky'}
];
Oh, and how's about this for succinctitude..
products =
[
['CA-XMC-00,'Shieldz for iPod mini - Ice'],
['The Recipe Manager','AS-TRM-100'],
['CA-XMC-10','Shieldz for iPod mini - Sky']
]products.i = {part_number:0,product_description:1}