•
•
•
•
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 397,602 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 2,667 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: 6028 | Replies: 10
![]() |
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Rep Power: 1
Solved Threads: 0
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'
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'
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation:
Rep Power: 5
Solved Threads: 47
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..
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.
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Rep Power: 1
Solved Threads: 0
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?
$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.
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation:
Rep Power: 5
Solved Threads: 47
Yes, that would work. You could also print output like this from PHP:
and then in javascript:
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.
new Array( "Saab", "Volvo", "BMW" )
var myArray = eval( xmlHttp.responseText );
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.
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation:
Rep Power: 5
Solved Threads: 47
Hm. there are apparantly no constructors for key/value type arrays ( hashes/dictionaries/w.e. you'd call them )
This might help you:
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.
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.
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation:
Rep Power: 5
Solved Threads: 47
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.
> 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.
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."
"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."
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 943
Reputation:
Rep Power: 5
Solved Threads: 47
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.
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.
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Rep Power: 1
Solved Threads: 0
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 + ")");
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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
- Previous Thread: How to make controls visible false using javascript...?
- Next Thread: HELP needed on javascript menus PLEASE...



Linear Mode