[RESOLVED] Very simple method doesn't work reliably
In a nutshell, I'm using JavaScript to populate and control select boxes in a form. What I'm doing is passing arrays from PHP to JavaScript so they can be accessible after the page loads into the browser.
This is how I do it...
PHP parses the array data into my JavaScript output
PHP Code:
// $js_content is passed to html within script tags
$js_content = "var dataArray = new Array();";
var categoryArray = new Array();
categoryArray['0'] = "Software Upgrade";
categoryArray['1'] = "Hardware Upgrade";
categoryArray['3'] = "Spare Parts";
categoryArray['4'] = "Warranty Parts";
categoryArray['5'] = "New Table";
categoryArray['6'] = "Refurbished Table";
categoryArray['7'] = "Maintenance";
categoryArray['8'] = "Return";
categoryArray['9'] = "Service Parts";
function setCategory(selection)
{
var myCategory = categoryArray[selection];
alert('categoryArray[' + selection + '] = ' + categoryArray[selection]);
switchMenu('Software Upgrade', 'none');
switchMenu('Hardware Upgrade', 'none');
switchMenu('Return', 'none');
if (currentTable == 'defaultValue')
{
alert('Table must be selected.');
document.new_request.categoryid[0].selected = '1';
}
else
{
switchMenu(myCategory, 'block');
}
}
var currentPartNo = '';
var partArray = new Array();
partArray['119'] = "Key for Bill Validator";
partArray['121'] = "Key for Power Door";
partArray['120'] = "Key For Shoe";
<!-- ect, ect... -->
function setPartNo(selection)
{
alert('partArray[' + selection + '] = ' + partArray[selection]);
document.new_request.partname.value = partArray[selection];
currentPartNo = selection;
setUnitPrice();
}
What I don't understand, though, is that the point at which it breaks down is only when I try to access the categoryArray. I've switched up the code to have it access other arrays and it works. Additionally, though, I've set other functions to reference the category array and they also malfunction. The only problem is that I can't find any errors.
Well, without testing the code, I have noticed something. Despite PHP, JavaScript make a subtle difference between a simple ordered list of values (array), and a collection of pairs property:value (object)
Array:
Code:
var array=[]; //short notation for var array=new Array()
array[0]='a';
array[1]='b';
array[2]='c';
Note the blue: Within the simple arrays, the identifier (the key) is a number, not a string. Well, JavaScript is tolerant in this case, an it will take it as a number, till the end. But, important: the last (the greatest) identifier sets the rank of the array. The identifier is not required; it can be implicit.
Code:
var array=['a','b','c']
Now, if you think like in PHP, you would expect like an array like:
I tried using non-quoted numbers, but the script never accepted them. It only started working once I started quoting the index keys to make the arrays associative.
I'll try the object method and return with my results.
Your code will work anyway, because all arrays are objects in JavaScript, but not necessarily as you intent. And yes, you can nest arrays and objects to your heart's content.
Great wit and madness are near allied, and fine a line their bounds divide.
But I'm still encountering the same malfunction. Whenever I reference the (now object) $categoryArray from within my function, it does nothing.
I disabled all of the script and had it run a simple alert box
HTML Code:
alert('setCategory('+ selection +')');
and it worked, it traced the user's selection, so I know that variable is functioning properly. Then I tried both the "categoryArray.selection" and "categoryArray[selection]" methods but neither worked. So finally I tried a static trace and tried "categoryArray.1", "categoryArray[1]", and "categoryArray['1']"; but to no avail.
I just noticed that I didn't have the terminating semicolon at the end there. I added it after the end bracket and ran my tests again and it still doesn't work.
I am sort of trying random things to see if they change anything and one of the things I tried was just tracing categoryArray with no selection and it returned "undefined"
I thought that was peculiar, so I commented out my definition of categoryArray and added a definition within the function; it is empty, but it is defined. Then when I traced it, it simply returned an empty string, but not "undefined".
Which leads me to conclude that for some reason my function cannot access the array existing in the parent scope even though my other functions can access their parent variables just fine.
That said, is there any specific method to accessing parent variables like a global call in PHP?
For example:
PHP Code:
function myFunct() { global $myVar; echo "Now I can access $myVar!"; }
I would just define it within the function, but I need that array accessible to other functions as well.
Edit: sorry for the multiple posts. I swear that edit button wasn't there last night when I looked for it, but I could have just been very tired.
On the other hand, when I DO move the array definition to within the function, it traces appropriately, returning each value of the array. I do not understand why it cannot find my array outside the function...
Last edited by Errant_Shadow; 07-29-2010 at 10:51 AM.
While exploring this, I've come across yet another oddity.
Even though I'm setting variables with defaults, they are tracing as undefined unless I set them at least once with their function. However, when I include a call to their function with a default value to set, it doesn't execute; it ONLY executes when called by the event handler attached to the form fields...
This still traces as "undefined" until I made a selection on the table field of my form.
HTML Code:
var currentTable = 'defaultValue';
function setTable(selection)
{
currentTable = selection;
alert('currentTable = ' + currentTable);
}
setTable('defaultValue');
I would just define it within the function, but I need that array accessible to other functions as well.
Define it outside the functions:
Code:
var myArray;
function bulidArray(){
for(var i=0;i<10;i++){
myArray[myArray.length]=i;
}
}
function showArray(){
alert(myArray)
}
buildArray();
showArray();
JavaScript uses 2 type of variables: global and local. The type is not set by a different token, it is set only by the position of the variable. If it is defined outside the functions, it is global. If it is defined inside the functions, it is local.
To define a variable, you have to use the token var. This token it is to be used only once, when the variable is defined.
Code:
var globalvariable='something'; // this is global
function MyFunction(){
var localVariable='something'; // this is local
}
Bookmarks