tags:

views:

170

answers:

5

I want to update two different values with one ajax-response.

<span id="nr1">Change this</span>
<span id="nr2">and change this</span>

Now I can just change one value, I do like this:

document.getElementById('nr1').innerHTML = xmlHttp.responseText;

Is it possible to do something like this:

document.getElementById('nr1').innerHTML = xmlHttp.responseText1;
document.getElementById('nr2').innerHTML = xmlHttp.responseText2;

** UPDATE ** The response comes from php. I'm totally new to JSON.

+1  A: 

If you trust and control the server, just return a dictionary in JSON for the response and use it on the client side. So:

v = eval(xmlHttp.responseText);
document.getElementById('nr1').innerHTML = v['nr1']
document.getElementById('nr2').innerHTML = v['nr2']
brool
Ok, not sure I get the hole picture. I use php. Can you give me an example on how the php can look?
Johan
+4  A: 

There are no responseText1 and responseText2 properties of an XMLHTTPRequest(which I assume your xmlHttp is), just responseText, so you have to return something parsable in that responseText field(like JSON). So you server may send back {"firstResponse":"value1","secondResponse":"value2"} and you can fill your fields from that JSON string. Use the json2.js library from json.org

<script type="text/javascript" src="json2.js"></script>
. . .
var theResponse = JSON.parse(xmlHttp.responseText);
document.getElementById('nr1').innerHTML = theResponse.firstResponse;
document.getElementById('nr2').innerHTML = theResponse.secondResponse;

EDIT: In order to craft this JSON response from PHP you should use the PHP JSON libraries. There are several examples in the json_encode page that can get you started. The other code I posted(and that is posted in other responses) are all browser side javascript code.

$arr = array ('firstResponse'=>'value1','secondResponse'=>'value2');
echo json_encode($arr);

Place that code into your PHP script to generate the JSON string

{"firstResponse":"value1","secondResponse":"value2"}

Then the previously posted javascript code will parse that.

ryanday
I try to do like you say. I get the right response from php: {"firstResponse":"value1","secondResponse":"value2"}. But it does not print out anything in span nr1 or nr2. Any idea what I do wrong?
Johan
Download json2.js from json.org, http://www.json.org/json2.js and use JSON.parse(xmlHttp.responseText) instead of .parseJSON(). I've been stuck with my companies internals too long.
ryanday
I've updated the code to reflect my comment
ryanday
Thanks a lot, it works just fine now :)
Johan
Great! Sorry for json.org bit, Glad it works!
ryanday
Johan
A: 

As already said it would make sense to return your Ajax call as a JSON Object. I recommend the more secure JSONP call (don't know if you can use any library that supports this natively).

// Your script returns this
callback123(
{
    "nr1" : "This is conten for nr1",
    "nr2" : "Some content for nr2"
});

// JavaScript callback looks like this
function callback123(data)
{
    for(var key in data)
     document.getElementById(key).innerHTML = data[key];
}
Daff
A: 

use JSON.parse

link text

A: 

good, tengkyu. I will try...

Lukman