Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I pass One JSON Record to a Function 1

Status
Not open for further replies.

LyndonOHRC

Programmer
Sep 8, 2005
603
US
I have a select list that is populated with JSON Data. I'm trying to pass each record to a function using the onclick event so I can populate several form fields with the data from the selected record. If this is possible how would I go about it?

This is what I've tried but I don't understand enough about the process to get it working.

build the list item(s):
Code:
var users = eval(trim(xmlhttp.responseText));
var str = '<ul>';
for ( var recno in users ) {
	str += '<li oonclick="putOwnerData( \'' +users[recno]+ '\')">'+users[recno].DisplayString+'</li>';
}
str +='</ul>';
Use the selected item:
Code:
function putOwnerData(JasonRecord) {
	var users = eval(JasonRecord);
	document.getElementById('OwnerLastName').value=users[0].LastName;
	document.getElementById('OwnerFirstName').value=users[0].FirstName;
	return true;
}

Thank you in advance...

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Is there another way I can ask this question?

Is it possible to pass an object to the next function as I'm trying to do? I need the JSON data in the onclick function that is called. Google gives no help, I must be asking incorrectly or it's not possible....

Any feedback would much appreciated.

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
I think its more likely the escaping of the quotes that may be causing issues. Json data is nothing more than a string that gets evaluated so it is completely possible to pass it. But quotes and other special characters may cause issues when the event is rendered.

Try looking at the resulting string once your loop is done (using an alert, or a output to screen) and see if there are any issues with it like overlapping quotes etc...

Also error checking is vital, IE, FF and even chrome have error checking consoles you can use.


By the way "oonclick" is an invalid event. One "O" too many.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
[0] >var users = eval(trim(xmlhttp.responseText));
Since you're not using json library, I can thereby suppose xmlhttp.responseText is what sometimes called json text and users is the parsed json object. First, you've to correct the obvious mistake. trim is not a js function.
[tt]
var jsontext=xmlhttp.responseText.replace(/^\s*|\s*$/,"");
var users=eval('('+jsontext+')');
[/tt]
[1]
>for ( var recno in users ) {
> str += '<li oonclick="putOwnerData( \'' +users[recno]+ '\')">'+users[recno].DisplayString+'</li>';
>}

[1.1] In this case you write users[recno] a component of json object without 'stringify'ing it. That would be a mistake. In the function putOwnerData() the parameter is again parsed by eval(). It implies that you have again to stringify each and every (array) entry. This is not a easy/simple task to script it yourself. In firefox, it has native support since 3.1. IE8 has native support of json too. They both provision static method of JSON.stringify() method. If your browser's version has not yet native support, you can always get the Crawford's json-js download and include the parse.js by script tag. (With that setup, you can also use the JSON.parse() method in [0], but that is just a bonus and a better way to guard against possible security loopholes.)

[1.2] With [1.1] in mind, you can use the JSON.stringify() function for the purpose. Furthermore, the return includes a wrapping pair of quotes, so you don't thereby need to add the wrapper quote/single-quote, but, still need quite a bit of care to choose the combination amongst the quote and single quote.
[tt]
for ( var recno in users ) {
str += "<li [red]on[/red]click='putOwnerData(" + JSON.stringify(users[recno])+ ")'>"+users[recno].DisplayString+"</li>";
}
[/tt]
 
This is great tsuji! Using the JSON.stringify() method the code is human readable now. LOL The browser is no issue as this code is only accessible on our intranet.

FYI:

trim() is a local function in my .js file. My server side language returns lot of white space before and after the actual data the becomes xmlhttp.responseText. So I always trim it to reduce the variable size.

While waiting for a solution I sent each of the nine elements I wanted to send to the putOwnerData() function as passed variables and it works great, however, code is quite inelegant and I would hate to have left it that way.

Thank you so much!

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel. Covey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top