Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Return Array from PHP script

    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???
    In love with PHP,Mysql & Javascript

  • #2
    New Coder
    Join Date
    Nov 2008
    Location
    New Delhi,India
    Posts
    26
    Thanks
    1
    Thanked 3 Times in 3 Posts
    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.

  • #3
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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
    Code:
    echo json_encode($var);
    will the output of this be directly available to ajax snippet or what??
    In love with PHP,Mysql & Javascript

  • #4
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Iam using foll in my PHP script:
    Code:
    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
    Code:
    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...
    In love with PHP,Mysql & Javascript

  • #5
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    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
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #6
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I gave up the idea of Json and am using now xml to fetch the result.Hwever,iam now stucked at:
    Code:
     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'
    Code:
     response=ajaxreq.responseXML;
      alert (response.getElementsByTagName("Category")[0]);
    Any ideas..
    In love with PHP,Mysql & Javascript

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by satish_j View Post
    Iam using foll in my PHP script:
    Code:
    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
    Code:
    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:
    Code:
    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

  • #8
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by satish_j View Post
    I gave up the idea of Json and am using now xml to fetch the result.Hwever,iam now stucked at:
    Code:
     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'
    Code:
     response=ajaxreq.responseXML;
      alert (response.getElementsByTagName("Category")[0]);
    Any ideas..
    Any help???
    Why do i get null value.ResponseXML also not working for me....
    In love with PHP,Mysql & Javascript

  • #9
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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:
    Code:
    <?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:
    Code:
    FictionNon-FictionAutobiography
    instead of:
    Code:
    <option>Fiction</option>
    <option>Non-Fiction</option>
    <option>Autobiography</option>
    Any inputs??
    In love with PHP,Mysql & Javascript

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by satish_j View Post
    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:
    Code:
    <?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:
    Code:
    FictionNon-FictionAutobiography
    instead of:
    Code:
    <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.

    Edit: 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

  • #11
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    this is not valid xml, it miss the root node, read a intro about xml, please.
    I get this error:
    Code:
    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??
    In love with PHP,Mysql & Javascript

  • #12
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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:
    Code:
      <?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....
    In love with PHP,Mysql & Javascript

  • #13
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by satish_j View Post
    I get this error:
    Code:
    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

  • #14
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    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...
    In love with PHP,Mysql & Javascript

  • #15
    New Coder
    Join Date
    Aug 2008
    Location
    India
    Posts
    88
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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...
    In love with PHP,Mysql & Javascript


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •