使用 Jquery Ajax 从 Mysql 中检索数据 [英] Using Jquery Ajax to retrieve data from Mysql

查看:35
本文介绍了使用 Jquery Ajax 从 Mysql 中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<头><script src="jquery-1.9.1.min.js"><脚本>$(document).ready(function() {var 响应 = '';$.ajax({类型:获取",url: "Records.php",异步:假,成功:功能(文本){回复 = 文字;}});警报(响应);});<身体><div id="div1"><h2>让 jQuery AJAX 更改此文本</h2>

<button>获取记录</button>

Records.php 是从 Mysql 中获取记录的文件.
数据库中只有两个字段:姓名"、地址".

<tr><td>名称:</td><td>地址:</td></tr><?phpwhile ($row = mysql_fetch_array($result)){echo "";echo "<td>$row[1]</td>";echo "<td>$row[2]</td>";回声</tr>";}?>

此代码无效.

解决方案

对于使用 Ajax + jQuery 检索数据,您应该编写以下代码:

 <script type="text/javascript" src="jquery-1.3.2.js"><script type="text/javascript">$(document).ready(function() {$("#display").click(function() {$.ajax({//创建一个ajax请求到display.php类型:获取",网址:display.php",dataType: "html",//期望返回 html成功:功能(响应){$("#responsecontainer").html(response);//警报(响应);}});});});<身体><h3 align="center">管理学生详细信息</h3><table border="1" align="center"><tr><td><input type="button" id="display" value="显示所有数据"/></td></tr><div id="responsecontainer" align="center">

对于mysqli连接,这样写:

为了显示数据库中的数据,你应该这样写:

";echo "<td align=center>$data[0]</td>";echo "<td align=center>$data[1]</td>";echo "<td align=center>$data[2]</td>";echo "<td align=center>$data[3]</td>";echo "<td align=center>$data[4]</td>";回声</tr>";}echo "</table>";?>

list.php: A simple ajax code that I want to display only records of the Mysql table:

<html>

<head>
    <script src="jquery-1.9.1.min.js">
    </script>
    <script>
    $(document).ready(function() {
        var response = '';
        $.ajax({
            type: "GET",
            url: "Records.php",
            async: false,
            success: function(text) {
                response = text;
            }
        });

        alert(response);
    });
    </script>
</head>

<body>
    <div id="div1">
        <h2>Let jQuery AJAX Change This Text</h2>
    </div>
    <button>Get Records</button>
</body>

</html>

Records.php is the file to fetch records from Mysql.
In the Database are only two fields: 'Name', 'Address'.

<?php
    //database name = "simple_ajax"
    //table name = "users"
    $con = mysql_connect("localhost","root","");
    $dbs = mysql_select_db("simple_ajax",$con);
    $result= mysql_query("select * from users");
    $array = mysql_fetch_row($result);
?>
<tr>
    <td>Name: </td>
    <td>Address: </td>
</tr>
<?php
    while ($row = mysql_fetch_array($result))
    {
        echo "<tr>";
        echo "<td>$row[1]</td>";
        echo "<td>$row[2]</td>";
        echo "</tr>";
    }   
?>

This code is not working.

解决方案

For retrieving data using Ajax + jQuery, you should write the following code:

 <html>
 <script type="text/javascript" src="jquery-1.3.2.js"> </script>

 <script type="text/javascript">

 $(document).ready(function() {

    $("#display").click(function() {                

      $.ajax({    //create an ajax request to display.php
        type: "GET",
        url: "display.php",             
        dataType: "html",   //expect html to be returned                
        success: function(response){                    
            $("#responsecontainer").html(response); 
            //alert(response);
        }

    });
});
});

</script>

<body>
<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>
</body>
</html>

For mysqli connection, write this:

<?php 
$con=mysqli_connect("localhost","root",""); 

For displaying the data from database, you should write this :

<?php
include("connection.php");
mysqli_select_db("samples",$con);
$result=mysqli_query("select * from student",$con);

echo "<table border='1' >
<tr>
<td align=center> <b>Roll No</b></td>
<td align=center><b>Name</b></td>
<td align=center><b>Address</b></td>
<td align=center><b>Stream</b></td></td>
<td align=center><b>Status</b></td>";

while($data = mysqli_fetch_row($result))
{   
    echo "<tr>";
    echo "<td align=center>$data[0]</td>";
    echo "<td align=center>$data[1]</td>";
    echo "<td align=center>$data[2]</td>";
    echo "<td align=center>$data[3]</td>";
    echo "<td align=center>$data[4]</td>";
    echo "</tr>";
}
echo "</table>";
?>

这篇关于使用 Jquery Ajax 从 Mysql 中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆