?
Solved

Retrieve json_encode values in javascript array

Posted on 2009-12-23
4
Medium Priority
?
1,575 Views
Last Modified: 2013-11-08
I am using following PHP code to store column values and pass to JSON function:

echo json_encode(array($PostedDate.$Places.$Company.$Designation.$ProjectDetails.$DesiredCandidate.$HRName.$HRContact.$Email));

The above thing is returned in a variable called 'data' to JSON function. I want to store the above values in JavsScript array? How to do that?
0
Comment
Question by:rpkhare
  • 2
3 Comments
 
LVL 8

Expert Comment

by:kebabs
ID: 26117973
The point of JSON is to have them stored as JS objects.

So if you have in your JavaScript:

var data = {"a":1, "b":2, "c":3};

You can then access it as such:

data.a

Let me know if that is fine. If you really want a JS array, you can loop over the array instead of running a json_encode() over it and output it with JS array syntax.
0
 
LVL 8

Author Comment

by:rpkhare
ID: 26123735
Following code calls the code I mentioned in my original post. Please let me know how to modify it?

				$(document).ready(function(){
					$("#Edit").click(function(){
							$.getJSON("fetchvalues.php?UpdateRecordID=" + $.cookie('UpdateRecordID'),
        					function(data){
							// Data retrieved in concatenated form. So we will break it and store values in array.
							var concatenatedvalues = new Array();
							concatenatedValues = data;
							alert(concatenatedValues.length);
						});
					});
				});

Open in new window

0
 
LVL 8

Accepted Solution

by:
rpkhare earned 0 total points
ID: 26124242
Ok, I solved this problem. Someone suggested me to use commas instead of period is this code:

echo json_encode(array($PostedDate.$Places.$Company.$Designation.$ProjectDetails.$DesiredCandidate.$HRName.$HRContact.$Email));

Now, from within I can call a column simply like:

alert(data[1]);

Please note that the data is a function used by JSON to retrieve results.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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
Course of the Month15 days, 19 hours left to enroll

850 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