LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2013, 11:22 AM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
JAVASCRIPT/JQUERY - Variable not referenced, not-global??


Hi,

The below code keeps giving me "p_labels not referenced". No matter where I put the <script> it will not work.

Not sure the switch statement will work either!

Script does access the .php file and return the proper JSON data.

Any help appreciated.

R

Code:
<!DOCTYPE HTML>
<html>
<head>

<title>Simple $.getJSON Example</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript" src="../jquery/jquery-1.5.1.js"></script>

<style type="text/css">

#ajaxBox { background-color:#FFFF99; margin:10px; border:thin solid #FF0000; width:60%; height:50%;}
#formHeader{text-align:center; font-size:18px; color:#0000FF;}
#myform{text-align:center;}

</style>

<script type="text/javascript">

p_labels=new Array(); // personal  I'VE TRIED var p_labels=new Array() ALSO!
a_labels=new Array(); // address
w_labels=new Array(); // work

p_labels["First_Name"]="First Name: ";
p_labels["Last_Name"]="Last Name: ";
p_labels["Display_Name"]="Display Name: ";
p_labels["Home_Phone"]="Phone: ";
p_labels["Home_Cell"]="Cell: ";
p_labels["Home_Email"]="Email: ";
p_labels["Home_Notes"]="Personal Notes: ";


a_labels["Home_Address"]="Address: ";
a_labels["Home_City"]="City: ";
a_labels["Home_State"]="State: ";
a_labels["Home_Zip"]="Zip Code: ";

</script>
	
<script type="text/javascript" >

$(document).ready(function() { //Finish loading the entire page before processing any javascript

$.getJSON("bluealert.php", processPeople);

function processPeople(data) {



$.each(p_labels,function(i, value){
	console.log('index: ' + i + ',value: ' + value);
	document.writeln(i +" .. "+ value);

	switch (i) {
		case i=="First_Name" :
			alert("ho ho ho " + value);
			break;
		default :
			alert("no match");
	} // end switch

}); // end each

} // end process people

}); // end ready


</script>

</head>

<body>

<div id="ajaxBox">


</div>




</body>
</html>
 
Old 09-26-2013, 06:28 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
1. Is there a reason why you use 2 script tags? Would be nicer if you declare the variables within the document.ready() function, wouldn't it?

2. try p_labels=new Object() instad of p_labels=new Array()

Arrays in javascript use numeric indexes. You may use objects similar to associative arrays.
I'm not sure whther this works but could be.
Cheers,j
 
Old 09-26-2013, 06:28 AM   #3
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
1. Is there a reason why you use 2 script tags? Would be nicer if you declare the variables within the document.ready() function, wouldn't it?

2. try p_labels=new Object() instad of p_labels=new Array()

Arrays in javascript use numeric indexes. You may use objects similar to associative arrays.
I'm not sure whther this works but could be.
Cheers,j
 
Old 09-27-2013, 06:14 AM   #4
heinblöd
Member
 
Registered: May 2004
Location: France
Distribution: Slackware Gentoo
Posts: 186

Rep: Reputation: 31
if you change 2 things the script will run :

1. Declare p_labels as Object. Jquery.each() expects an Object, not: an Array.
http://api.jquery.com/jQuery.each/
Code:
var p_labels=new Object(); // personal    
var a_labels=new Object(); // address
var w_labels=new Object(); // work
2. The is an error inside the switch statement :
It should be
Code:
$.each(p_labels,function(i, value){
	console.log('index: ' + i + ',value: ' + value);
	document.writeln(i +" .. "+ value);

	switch (i) {
		case i = "First_Name" : //<<HERE! it's not a comparison so not case i == "First_Name"
			alert("ho ho ho " + value);
			break;
		default :
			alert("no match");
	} // end switch
With this two modifs it runs here, althought I can't judge, if $.getJSON returns the data you want it to


EDIT: The api says "jQuery.each( collection, callback(indexInArray, valueOfElement) )" and a collection can be an array or an object.
So what I wrote before is not 100% correct.
BUT this example works with Object but it doesn't when you change the type to Array.
I guess this is caused by the way p_labels is created.
The example given in the Api explains this, I think:
Code:
<script>
var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };
jQuery.each( arr, function() {
$( "#" + this ).text( "Mine is " + this + "." );
// Will stop running after "three"
return ( this !== "three" );
});
jQuery.each( obj, function( i, val ) {
$( "#" + i ).append( document.createTextNode( " - " + val ) );
});
</script>

Last edited by heinblöd; 09-27-2013 at 06:23 AM. Reason: Correction
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript :: jQuery :: .animate() Causing Problems... cin_ Programming 0 06-07-2013 02:44 PM
Global Objects in Javascript- Possible to Exploit? dman777 Programming 5 07-13-2012 09:17 AM
non-static variable $SwitchMap$com$ibot$util$Paths$OS cannot be referenced from ... Infasoft Programming 5 07-16-2010 05:03 PM
Java non-static variable cannot be referenced from a static context issue rickrvo Programming 2 02-12-2010 03:26 PM
What is Json and jQuery and how are they related to Javascript? Romanus81 Programming 1 08-13-2009 09:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration