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 373,930 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,245 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

returning array from ajax.responseText?

Join Date: Jul 2006
Location: Deptford, London
Posts: 916
Reputation: MattEvans will become famous soon enough MattEvans will become famous soon enough 
Rep Power: 5
Solved Threads: 46
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
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  
All times are GMT -4. The time now is 5:58 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC