0

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???

1 Answer 1

1

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..

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.