使用Jquery Ajax从Mysql中检索数据

list.php :一个简单的ajax代码,我只想显示Mysql表的记录:

      $(document).ready(function(){ var response = ''; $.ajax({ type: "GET", url: "Records.php", async: false, success : function(text) { response = text; } }); alert(response); });    

Let jQuery AJAX Change This Text

Records.php是从Mysql获取记录的文件。
在数据库中只有两个字段:“名称”,“地址”。

    Name:  Address:   <?php while ($row = mysql_fetch_array($result)) { echo ""; echo "$row[1]"; echo "$row[2]"; echo ""; } ?> 

此代码无效。

要使用Ajax + jQuery检索数据,您必须编写以下代码:

      

Manage Student Details

对于mysqli连接,请写下:

  

要显示数据库中的数据,您必须写下:

    Roll No Name Address Stream Status"; while($data = mysqli_fetch_row($result)) { echo ""; echo "$data[0]"; echo "$data[1]"; echo "$data[2]"; echo "$data[3]"; echo "$data[4]"; echo ""; } echo ""; ?> 

您不能返回ajax返回值。 存储全局变量存储返回后的返回值。
或者像这样更改你的代码。

 AjaxGet = function (url) { var result = $.ajax({ type: "POST", url: url, param: '{}', contentType: "application/json; charset=utf-8", dataType: "json", async: false, success: function (data) { // nothing needed here } }) .responseText ; return result; } 

请确保你的$row[1] , $row[2]包含正确的值,我们假设1 = Name , and 2 here is your Address field

假设您已经从Records.php中正确获取了记录,您可以执行以下操作:

 $(document).ready(function() { $('#getRecords').click(function() { var response = ''; $.ajax({ type: 'POST', url: "Records.php", async: false, success : function(text){ $('#table1').html(text); } }); }); } 

在你的HTML中

  //Let jQuery AJAX Change This Text 

一点点说明:

尝试学习PDO http://php.net/manual/en/class.pdo.php,因为不再鼓励mysql_* functions

 $(document).ready(function(){ var response = ''; $.ajax({ type: "GET", url: "Records.php", async: false, success : function(text) { response = text; } }); alert(response); }); 

需要是:

 $(document).ready(function(){ $.ajax({ type: "GET", url: "Records.php", async: false, success : function(text) { alert(text); } }); });