Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用ajax从数据库检索特定数据_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

Javascript 如何使用ajax从数据库检索特定数据

Javascript 如何使用ajax从数据库检索特定数据,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,这是我从php文件检索所有数据的ajax代码: <script type="text/javascript"> $(document).ready(function() { $("#display").click(function() { $.ajax({ //create an ajax request to load_page.php type: "GET", url: "read.php

这是我从php文件检索所有数据的ajax代码:

<script type="text/javascript">
 $(document).ready(function() {
    $("#display").click(function() {                
      $.ajax({    //create an ajax request to load_page.php
        type: "GET",
        url: "read.php",             
        dataType: "html",   //expect html to be returned                
        success: function(response){                    
            $("#responsecontainer").html(response); 
            //alert(response);
        }
    });
});
});
</script>
<h3 align="center">Manage Student Details</h3>
<table border="1" align="center">
   <tr>
       <td> <input type="button" id="display" value="Display All Data" /> </td>
   </tr>
</table>
<div id="responsecontainer" align="center"></div>

$(文档).ready(函数(){
$(“#显示”)。单击(函数(){
$.ajax({//创建一个ajax请求以加载\u page.php
键入:“获取”,
url:“read.php”,
数据类型:“html”,//希望返回html
成功:功能(响应){
$(“#responsecontainer”).html(响应);
//警报(响应);
}
});
});
});
管理学生详细信息
这是我的php文件的一部分,它从数据库中检索数据并将其存储到变量中:

    <?php
  while($row = mysqli_fetch_assoc($result)){
    $user_id = $row["user_id"]
    $user_name = $row["user_name"]
    $user_text = $row["user_text"]
    }
    ?>

如果我回显上述变量,那么它们将显示在我的html页面中,其中包含ajax代码,但我希望使用ajax获取每个变量,并对它们执行一些操作,然后在我的html页面中显示它们
php中有简单的HTMLDOM来获取一个页面的html元素,有没有类似于用于ajax的php简单HTMLDOM的东西?如果没有,那怎么可能做到我说的事情呢?

如果有人能帮我,我将不胜感激:)

您必须返回带有变量的JSON对象。在while循环外创建一个数组,然后在每个while循环上执行array_推送到主数组。循环结束后,通过json对数组进行回显编码,然后在ajax端对其进行解码。

服务器端

$array = array();
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)){

                        array_push(//what you need);

                }
echo json_encode($array);
在客户端

success: function(response){                    
            data = $.parseJSON(JSON.stringify(returnedData)); 

        }
请注意,JSON.stringify不是必需的,但我喜欢它 数据现在是一个对象,您可以使用data.propertyname访问其属性