I have generated a php array from oracle db (index.php)

    while ($row = oci_fetch_array ($stid, OCI_ASSOC + OCI_RETURN_NULLS)){

    $result[]=$row;

}

Now i have to pass this array to another php file for other processing.

 <script language="javascript" type="text/javascript">

  function sortme()
   {  
    var d=document.getElementById("div");
    var obj = <?php echo json_encode($result); ?>;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
           xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
   xmlhttp.onreadystatechange=function()
     {
       if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        d.innerHTML=xmlhttp.responseText;
        }
     }
  xmlhttp.open("GET","getsorted.php?q=obj",true);
  xmlhttp.send();
   }

Then in the php file get sorted.php i need this double dimension array "result" back and sort it. So I use this, (getsorted.php)

   <?php

    $q=$_GET["q"];
    $ans=json_decode(str_replace('\"','"',$q),true);
    var_dump($q);
    var_dump($ans);
   ?>

But i am not able to figure out anything.I am not able to retrieve the array back. If this is the wrong approach please suggest a correct approach.

Please do post the code.

Thanks a lot.

IS there a better way to do it???

share|improve this question
feedback

1 Answer

You are passing the string "obj" when you have to pass the URLEncoded JSON representation. Replace:

xmlhttp.open("GET","getsorted.php?q=obj",true);

with:

xmlhttp.open("GET","getsorted.php?q=" + encodeURIComponent(JSON.stringify(obj)),true);

What you are doing is somewhat like this:

PHP file  AnotherPHPFile
   |           ^  |
   |       ____|  |
   V      /       v
Javascript---(sorted result into HTML)   

How about simply including the getsorted.php file in your index.php and straightaway use the sorting functionality. You save a network request that way..

share|improve this answer
include 'getsorted.php'; – Luuk van Egeraat Jul 19 at 11:30
Thanks, 2 Questions. Firstly this does not work :(. Secondly, how shall i include this getsorted in the same file.I am calling the javascript function on click of a table header to sort the table with respect to that header.So without javascript how do I do it? – Atanu Jul 19 at 11:35
Ignore the second part of the answer if you are going to fire the AJAX request upon the click of a button. First part still holds. What is the exact problem? Do you see the request going (and the corresponding response) in the browser's inspector? – Thrustmaster Jul 19 at 11:44
After I replace it with encodeURIComponent the request is not even going to the getsorted.php file. I am pretty sure about that.If i dont put encodeURI it calls getsorted and if I var_dump($q) its prints [object Object][object Object]......the number of times as many rows are there in the array. At any point in time I am not able to see the original array. Please help me out with this – Atanu Jul 19 at 11:57
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.