User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 330,218 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,876 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 3432 | Replies: 10
Reply
Join Date: Oct 2007
Posts: 34
Reputation: adrive is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
adrive adrive is offline Offline
Light Poster

returning array from ajax.responseText?

  #1  
Nov 28th, 2007
hi again,

i'm wondering if it's possible to return an array from my php generated content to ajax's responseText. I tried echoing back the whole array variable but can't seem to fully receive it in javascript.

somewhere in file1.php

<?php
$myArray = array();

$myArray['name'] = "ajax tech";
$myArray['age'] = 20;

echo $myArray;

?>


somewhere in the page that i need the data to display :

//(javascript, ajax = xmlHttp)

var myArray = new Array();
myArray = xmlHttp.responseText;

alert(myArray); //will alert 'Array'

alert(myArray[0]); //alerts 'undefined'
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 899
Reputation: MattEvans will become famous soon enough MattEvans will become famous soon enough 
Rep Power: 4
Solved Threads: 43
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Practically a Posting Shark

Re: returning array from ajax.responseText?

  #2  
Nov 29th, 2007
Eek.. again you need a way of determining IN JAVASCRIPT what the contents of a response represent. Semantically, there is no difference between an echoed PHP array and a line of plain text. All you are doing at the moment is reading the response text ( a string ) and trying to implicity cast it to a javascript array - this won't work.

When PHP echoes an array, it just prints out text to be output at the browser; at the javascript end you'd need to interpret that array using text manipulation functions or some other processing method, in order for that plain text to be a meaningful javascript array.

Just remember, the output from a typical server-side application doesn't have the same meaning as the running code that generated it; it has whatever meaning you choose to give it, but you have to choose how to deal with it, in order to give it any meaning atall.

That aside.. can you post the output from your PHP page? I'm not in the mood to run PHP code this morning, but I want to see how close to interpretable an array dump is..
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Oct 2007
Posts: 34
Reputation: adrive is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
adrive adrive is offline Offline
Light Poster

Re: returning array from ajax.responseText?

  #3  
Nov 29th, 2007
do you mean the code that produces the output for javascript?

$user = null;

$userData = array();

try
{

if(isset($_SESSION['name'])){
$userData['name'] = $_SESSION['name'];
}
if(isset($_SESSION['userid'])){
$userData['userid'] = $_SESSION['userid'];
}

}
catch(Exception $e)
{
logger($e->getMessage());
echo "Error : " .$e->getMessage();
}

echo $userData;

not much use, it just produces 'Array' plain text. If that's the case, do you mean i'll actually have to organise my own data into an understandable structure, and then chop it out from javascript and pass back to an array manually?

such as from php,

echo $myArrayStr = "name='mr box' | age=20 | sanity = 'not really' ";

then in javascript,

maybe some sorta split function that separates the "|" delimeter and assign them all into javascript?
Last edited by adrive : Nov 29th, 2007 at 3:28 am.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 899
Reputation: MattEvans will become famous soon enough MattEvans will become famous soon enough 
Rep Power: 4
Solved Threads: 43
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Practically a Posting Shark

Re: returning array from ajax.responseText?

  #4  
Nov 29th, 2007
Yes, that would work. You could also print output like this from PHP:
new Array( "Saab", "Volvo", "BMW" )
and then in javascript:
var myArray = eval( xmlHttp.responseText );
The eval function will treat the plaintext as javascript; returning an array type object in this case. That'd work for a numerically indexed array, you're using string indicies but you can use the same principle,

Don't use this principle the other way round whatever you do; that is, don't send textcode from client to server and evaluate it. That's the biggest security hole you could possibly make.
Last edited by MattEvans : Nov 29th, 2007 at 4:16 am.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 899
Reputation: MattEvans will become famous soon enough MattEvans will become famous soon enough 
Rep Power: 4
Solved Threads: 43
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Practically a Posting Shark

Re: returning array from ajax.responseText?

  #5  
Nov 29th, 2007
Hm. there are apparantly no constructors for key/value type arrays ( hashes/dictionaries/w.e. you'd call them )

This might help you:

<html>
<body>

<script type="text/javascript">
function mkhash( )
{
  var ret = new Object( );
  for (var i = 0; i < arguments.length; ++i )
  {
    ret[arguments[i][0]] = arguments[i][1];
  }
  return ret;
}

var myhash = eval( 'mkhash( [ "name", "matt" ], [ "age", "22" ] )' );

document.writeln( myhash[ "name" ] );
document.writeln( myhash[ "age" ] );
</script>

</body>
</html>

As long as you print out a call to mkhash from PHP, i.e.:

echo "mkhash( [ 'name', 'mr box' ], [ 'age', '20' ], [ 'sanity', 'not really' ] )";

when you eval that in javascript, js will 'find' the mkhash function if it's present on the page, and use that to generate the key-value type array object.

Alternatively, if you just print:

[ 'name', 'mr box' ], [ 'age', '20' ], [ 'sanity', 'not really' ]

from PHP, you can then evaluate it like:

var myhash = eval( 'mkhash('+xmlhttp.responseText+')' );

and then use name-based indexing in JS. Note, that works because [ "a", "b".. etc ] is a shorthand constructor for a javascript array - in this case you're only using them to indicate the key/value pairs in a hash.

good luck, anyway.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 899
Reputation: MattEvans will become famous soon enough MattEvans will become famous soon enough 
Rep Power: 4
Solved Threads: 43
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Practically a Posting Shark

Re: returning array from ajax.responseText?

  #6  
Nov 29th, 2007
Actually, it seems that there are shorthand key/value array constructors; and this is what digital-ether suggested in your other thread of this nature: http://en.wikipedia.org/wiki/JSON. That format is directly interpretable by javascript.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,611
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 21
Solved Threads: 297
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: returning array from ajax.responseText?

  #7  
Nov 29th, 2007
> it seems that there are shorthand key/value array constructors
Actually it's called an 'object literal'.

Encode the data you want to send by using a PHP binding for JSON at the server and decode the same using Douglas Crockfords' Javascript library for JSON.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Oct 2007
Posts: 34
Reputation: adrive is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
adrive adrive is offline Offline
Light Poster

Re: returning array from ajax.responseText?

  #8  
Nov 29th, 2007
Thanks guys, i'll try this up.

Is JSON widely used? I'm trying to use pure javascript without importing any third party components though, unless necessary.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 899
Reputation: MattEvans will become famous soon enough MattEvans will become famous soon enough 
Rep Power: 4
Solved Threads: 43
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Practically a Posting Shark

Re: returning array from ajax.responseText?

  #9  
Nov 30th, 2007
Well, data in that JSON format is valid, runnable javascript; the libraries that ~s.o.s~ suggests are just helper utilities.

A client-side library for interpreting the JSON safely is a good idea; since it's not totally safe to run code that you can't guarantee hasn't been tampered with.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Oct 2007
Posts: 34
Reputation: adrive is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
adrive adrive is offline Offline
Light Poster

Re: returning array from ajax.responseText?

  #10  
Dec 2nd, 2007
thanks guys, after trying out few suggested methods here, jSON is the most superior method since it takes into consideration strings with quotes.

S.O.S, why am i able to decode the encoded json string directly using eval without Douglas Crockfords' Javascript library for JSON. ?

on my php, im just using simple json_encode($mydata);

on javascript, myData= eval("(" + xmlHttp.responseText + ")");
Last edited by adrive : Dec 2nd, 2007 at 11:08 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Marketplace (Sponsored Links)
Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 3:45 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC