Solved

Correct javascript syntax to extract jSON array data?

Posted on 2016-08-29
1
114 Views
Last Modified: 2016-08-29
Experts,

I'm using jQuery's $.getJSON() function to pull data from my MySQL db.

I am able to return a proper JSON array via my PHP file but, I can't figure out how to call the data stored in the array?

I'm attempting to place the first and last name found within the jSON array into an input "$('#'+update_value)".

The values being placed into the input show as "undefined undefined".

jQuery Function
//////////////////////////////////////
//FIND EACH SPONSOR OR ALT USER NAME
$.each($(".alt_user, .sponsor"), function(){
	var NUID = $(this).val();
	var update_value = $(this).attr('update_value');
	if(NUID != ''){
		$.getJSON(
			"scripts/find_nuid.php", 
			{
				term:NUID
			}, 
			function(result){
				$('#'+update_value).val(result.first_name+' '+result.last_name);
			}//END FUNCTION
		);
	}//END IF
});

Open in new window


Example JSON array produced from the PHP file:
[{"first_name":"Joe","last_name":"Blow","nuid":"X123456"}]

Open in new window

0
Comment
Question by:evibesmusic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 41775683
JSON is an object, not an array. Your example shows a JSON object embedded in an array. The JSON object is...
{"first_name":"Joe","last_name":"Blow","nuid":"X123456"}

Open in new window

The square brackets on each end are unnecessary. You must remove those square brackets at each end of the returned value in your PHP file for your existing jQuery code to work. Otherwise I would expect the getJSON call to fail because the result is not a properly formatted JSON object.
0

Featured Post

Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question