View Full Version : Return Array from PHP script
satish_j
12-11-2009, 11:57 AM
My question is related to the PHP script that is called from Ajax.Till now,i have been using PHP scripts that used 'echo' function to return data to calling ajax script(and ultimately to browser).
I know 'echo' displays everything after it directly to browser..
But,i need some way of returning an Array from PHP script to the calling Ajax script without displaying anything on browser.
How to achieve same??Any ideas???
Since php and javascript are different languages having their own syntax, you will have to first convert your data into a format that javascript understands (like json).
So, instead of directly doing echo from your php script, first convert it to json using function json_encode and then do the echo.
Now in javascript you can parse that json and use in your code.
satish_j
12-11-2009, 12:33 PM
Thks v08i for quick resp..can you tell me how to use this json_encode array in AJAX code snippet?I mean if i use
echo json_encode($var);
will the output of this be directly available to ajax snippet or what??
satish_j
12-12-2009, 06:39 AM
Iam using foll in my PHP script:
echo json_encode($catgrs);
$catgrs is an array which is getting populated in PHP script
And,then in ajax script,iam trying to get this array through
var response=new Array();
response=eval('('+ajaxreq.responseText+')');
alert (response[0]);
But,i do not get any value.'Undefined' is what i get in ajax script..
Someone pls help...
Zangeel
12-12-2009, 07:02 AM
In PHP you can serialize an array, which makes it easy to store array data. I googled on how to unserialize the array using javascript and came across this, may be useful http://www.phpguru.org/static/PHP_Unserialize.html
Basically serialize in php, unserialize in javascript
satish_j
12-12-2009, 08:03 AM
I gave up the idea of Json and am using now xml to fetch the result.Hwever,iam now stucked at:
response=ajaxreq.responseXML;
alert (response.getElementsByTagName("Category"));
If is use above line,i get alert as 'Object'..But if i use below line,then i get 'Null'
response=ajaxreq.responseXML;
alert (response.getElementsByTagName("Category")[0]);
Any ideas..
oesxyl
12-12-2009, 08:57 AM
Iam using foll in my PHP script:
echo json_encode($catgrs);
$catgrs is an array which is getting populated in PHP script
And,then in ajax script,iam trying to get this array through
var response=new Array();
response=eval('('+ajaxreq.responseText+')');
alert (response[0]);
But,i do not get any value.'Undefined' is what i get in ajax script..
Someone pls help...
try this:
var response = eval(ajaxreq.responseText);
alert(response.toString());
before that try to see what you have in php using print_r before json_encode.
best regards
satish_j
12-12-2009, 10:00 AM
I gave up the idea of Json and am using now xml to fetch the result.Hwever,iam now stucked at:
response=ajaxreq.responseXML;
alert (response.getElementsByTagName("Category"));
If is use above line,i get alert as 'Object'..But if i use below line,then i get 'Null'
response=ajaxreq.responseXML;
alert (response.getElementsByTagName("Category")[0]);
Any ideas..
Any help???
Why do i get null value.ResponseXML also not working for me....:(:(
satish_j
12-12-2009, 10:57 AM
I think i've narrowed down the cause of issue.My php script is not giving xml formatted output.Here is what my php script look like:
<?php
header('Content-type: text/xml');
?>
<?xml version="1.0" encoding="UTF-8"?>
<?php
if(isset($_GET['prod'])) $prod=$_GET['prod'];
require('openDB.php');
$catgrs=array();
$query1="select category from categories where applies_to=(select ProdId from products where product='$prod')";
$result1=mysql_query($query1) or die("Error executing the query");
while($row=mysql_fetch_row($result1)) {
array_push($catgrs,$row[0]); }
for($i=0;$i<count($catgrs);$i++) {
echo "<option>".$catgrs[$i]."</option>"; }
require('closeDB.php');
?>
When i run this directly in browser,i get foll:
FictionNon-FictionAutobiography
instead of:
<option>Fiction</option>
<option>Non-Fiction</option>
<option>Autobiography</option>
Any inputs??
oesxyl
12-12-2009, 11:53 AM
I think i've narrowed down the cause of issue.My php script is not giving xml formatted output.Here is what my php script look like:
<?php
header('Content-type: text/xml');
?>
<?xml version="1.0" encoding="UTF-8"?>
<?php
if(isset($_GET['prod'])) $prod=$_GET['prod'];
require('openDB.php');
$catgrs=array();
$query1="select category from categories where applies_to=(select ProdId from products where product='$prod')";
$result1=mysql_query($query1) or die("Error executing the query");
while($row=mysql_fetch_row($result1)) {
array_push($catgrs,$row[0]); }
for($i=0;$i<count($catgrs);$i++) {
echo "<option>".$catgrs[$i]."</option>"; }
require('closeDB.php');
?>
When i run this directly in browser,i get foll:
FictionNon-FictionAutobiography
instead of:
<option>Fiction</option>
<option>Non-Fiction</option>
<option>Autobiography</option>
Any inputs??
this is not valid xml, it miss the root node, read a intro about xml, please.
xml tags are there but you can't see them in browser this way, take a look to the source of the page
best regards
satish_j
12-14-2009, 07:54 AM
this is not valid xml, it miss the root node, read a intro about xml, please.
I get this error:
Only one top level element is allowed in an XML document. Error processing resource 'http://localhost/ajax/getCategories.php'
<option>Fiction</option><option>Non-Fiction</option><option>Autobiography</option>
As you can see,i also get the output frm php script.Is this output not in correct xml format??
satish_j
12-14-2009, 09:55 AM
Parsing XML issue is also resolved.ican see the proper xml file in browser when run the php script generating the xml file.I have now the foll xml file:
<?xml version="1.0" encoding="UTF-8" ?>
- <Cats>
<option>Fiction</option>
<option>Non-Fiction</option>
<option>Autobiography</option>
</Cats>
Now,in my ajax script,if i alert response.getElementsByTagName("Cats")[0],i get alert as 'Object'...but response.getElementsByTagName("option")[0] gives me Null.
Any help???I think iam close to solving it....
oesxyl
12-14-2009, 09:57 AM
I get this error:
Only one top level element is allowed in an XML document. Error processing resource 'http://localhost/ajax/getCategories.php'
<option>Fiction</option><option>Non-Fiction</option><option>Autobiography</option>
As you can see,i also get the output frm php script.Is this output not in correct xml format??
yes, xml is not valid. You need to have a root( top level) element.
If you want a select element in the page, add a <select name="whatever"> node before first option element and </select> after last option. You don't need responseXML for that, you can use responseText.
best regards
satish_j
12-14-2009, 10:06 AM
yes, xml is not valid. You need to have a root( top level) element.
If you want a select element in the page, add a <select name="whatever"> node before first option element and </select> after last option. You don't need responseXML for that, you can use responseText.
incorrect xml issue has resolved..
Iam not getting alert for response.getElementsByTagName("option")[0].It gives Null as alert in IE,whereas FF displays proper value..
Any inputs...
satish_j
12-14-2009, 01:43 PM
with responsetext,innerHTML is not working with IE...with responseXML,getElementsbyTagName is returning Null with IE
I think i will go mad handling this problem.
Now,can anyone share a cross-browser ajax script to populate a dropdown list on selecting a value from 1st dropdown list...
satish_j
12-16-2009, 09:47 AM
Strange,if i change the output of php file to static values like this:
...
...
$result1=mysql_query($query1) or die("Error executing the query");
while($row=mysql_fetch_row($result1)) {
array_push($catgrs,$row[0]); }
echo "<Cats>";
/*for($i=0;$i<count($catgrs);$i++) {
echo "<options>".$catgrs[$i]."</options>"; }*/
echo "<options>check</options>";
echo "<options>check2</options>";
echo "</Cats>";
require('closeDB.php');
?>
then it works in ajax script.I get alert as 'check' on:
alert (resp.getElementsByTagName("Cats")[0].childNodes[0].firstChild.data);
Now,i can only conclude that php script is the cause of problem and not getElementsByTagname call...
Any inputs now...
satish_j
12-17-2009, 09:58 AM
After further troubleshooting,i narrowed it further to the array being called from PHP script:
I ENTERED 'print_r($catgrs)' in the php script and found that it is returned EMPTY to ajax script,but it is filled properly if i run it directly in browser...
This is getting weird now..If i run the php script directly in browser,i get the neatly formed xml file with all the values from DB..But,if i alert responseText from ajax script,i get foll
<?xml version="1.0" encoding="ISO-8859-1"?>
<Cats>
Array
(
)
</Cats>
satish_j
12-22-2009, 10:35 AM
Prob solved..the issue was in the ajax script.the GET variable send with url was empty...very silly:
var product=document.getElementById('prods').options[prod_idx].value
changed it to:
var product=document.getElementById('prods').options[prod_idx].text
Thanks to all for your time..Now,how do i mark this thread as solved..
replicahome
01-02-2010, 02:28 AM
I would also like to ask the question.mark and study.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.