将结果从PHP类传递到AJAX并返回到调用脚本

I have a class which returns a set of data from the database. The class is called by ajax and the results need to be returned back to the index.php so that I can format and display the results in a table format.

Problem: I'm unable to return the results throug ajax and back to a php variable. Any help you can provide would be greatly appreciated.

<php
class Questions 
{  public function displayQuestions()
   {   
     return $this->questionArray;   
   } // contains set of data from db
}
?>

Return the dataset from the class and pass it to the $var so that I can format the data for display

index.php:

<html>
<body>
<div id="questiondev" ><?php $var[] = returned by ajax ?> </div> 



<div id="questionButton">
   <form method="POST" name="form_questions" action="">
      <TEXTAREA NAME="saveqa"  id="saveqa"></TEXTAREA>
       <BUTTON class="btn_save" id ="btn_save" NAME="btn_save">Ask</BUTTON>
   </form>
</div>  
    <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script>           
          $(document).ready(function() { 
          $('#btn_save').on('click', function() { 
          $.ajax({
            type: "POST",
            cache: false, 
            url: "testData.php",
            dataType: "json",
            success:function(info){
            $('#questiondev').html(info[0]);
            console.log(" reponse :"+ info);
            }
         });
        }); 

        $('#btn_save').trigger("click");
         });                  
        </script> 
</body>
</html>

add

data:$("form").serialize(), U need to serialize the form

<div id="questionButton">
   <form method="POST" name="form_questions" action="">
      <TEXTAREA NAME="saveqa"  id="saveqa"></TEXTAREA>
       <BUTTON class="btn_save" id ="btn_save" NAME="btn_save">Ask</BUTTON>
   </form>
</div>  
    <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script>           
          $(document).ready(function() { 
          $('#btn_save').on('click', function() { 
          $.ajax({
            type: "POST",
            cache: false, 
            url: "testData.php",
            data:$("form").serialize(),
            dataType: "json",
            success:function(info){
            $('#questiondev').html(info[0]);
            console.log(" reponse :"+ info);
            }
         });
        }); 

        $('#btn_save').trigger("click");
         });                  
        </script> 
</body>
</html>

It doesn't look like you're echoing your results in json format. It appears that if your query is successful, $questionArray gets set, but is not echoed. Plus you can't just echo $questionArray - it must be output in json format for the Ajax code to accept it.

Try this - after $questionArray is set:

 $encodedJSON = json_encode($questionArray);
 echo $encodedJSON;

you can't insert result of AJAX request into PHP variable like this. When you run php page web server willl render it, and then javascript is run by browser, which means, that you can't edit PHP variables from javascript, because PHP runs on server and JS runs on client.

Why do you want to do this? Maybe it is flaw in app design, give more information and maybe we can help you more so you won't need to do this. If you want to format data, then format them before you send them back to AJAX :)