Chỉ mục.php
& nbsp; Tôi đã tạo một kết nối với cơ sở dữ liệu, sau đó tôi đã thiết kế bảng thông qua & nbsp; & nbsp; và & nbsp ;. Sau đó, tôi đã tạo một biến & nbsp; $ action & nbsp; để có được dữ liệu thông qua ajax. Sau đó tôi đã sử dụng & nbsp; mysqli_fetch_array & nbsp; để tìm nạp dữ liệu từ cơ sở dữ liệu.
<?php //including the database connection file include_once("config.php"); //fetching data in descending order (lastest entry first) //$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated // using mysqli_query instead ?> <html> <head> <title>Homepage</title> <link rel="stylesheet" href="DataTables/datatables.css" type="text/css"> <link rel="stylesheet" href="DataTables/DataTables/css/dataTables.bootstrap.css" type="text/css"> <link rel="stylesheet" href="DataTables/DataTables/css/jquery.dataTables.css" type="text/css"> <script src="DataTables/datatables.js"></script> <script src="style/jquery-3.2.1.js"></script> <script src="style/datatable.js"></script> <script src="DataTables/DataTables/js/dataTables.bootstrap.js"></script> <script src="DataTables/DataTables/js/jquery.dataTables.js"></script> </head> <body> <a href="add.html">Add New Data</a><br/><br/> <table id="datatable" class="display" width='100%' border=0> <thead> <tr bgcolor='#CCCCCC'> <td>Name</td> <td>Age</td> <td>Email</td> <td>Update</td> </tr> </thead> <?php //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array //$action=$_POST["action"]; //if($action=='showroom') { $result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC"); while($res = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>".$res['name']."</td>"; echo "<td>".$res['age']."</td>"; echo "<td>".$res['email']."</td>"; echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>"; } } ?> </table> </body> </html>Add.html
<html> <head> <title>Add Data</title> <script src="style/jquery-3.2.1.js"></script> <script src="style/insert.js"></script> <script src="style/view.js"></script> </head> <body> <a href="index.php">Home</a> <br/><br/> <table bgcolor="orange" align="center" width="25%" border="0"> <tr> <td>Name</td> <td><input type="text" name="name" id="name"></td> </tr> <tr> <td>Age</td> <td><input type="text" name="age" id="age"></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" id="email"></td> </tr> <tr> <td></td> <td><input type="submit" name="Submit" id="submit" value="Add"></td> </tr> </table> <button type="button" id="submitBtn">Show All</button> <div id="content"></div> </body> </html>view.js
Tôi tìm fetchd dữ liệu & nbsp; từ cơ sở dữ liệu. và & nbsp; đã sử dụng chức năng & nbsp; show_all () & nbsp; sau đó tôi gọi là $. Lần đầu tiên tôi cố gắng tìm nạp dữ liệu từ cơ sở dữ liệu thông qua AJAX.
$(document).ready(function(e) { $('#submitBtn').click(function() { debugger; $.ajax({ //data :{action: "showroom"}, url :"index.php", //php page URL where we post this data to view from database type :'POST', success: function(data){ $("#content").html(data); } }); }); });Có thể ai đó xin vui lòng giúp tôi với điều này?
AJAX có thể được sử dụng để giao tiếp tương tác với cơ sở dữ liệu.
Ví dụ cơ sở dữ liệu AJAX
Ví dụ sau đây sẽ chứng minh làm thế nào một trang web có thể tìm nạp thông tin từ cơ sở dữ liệu với AJAX:
Thí dụ
Thông tin người sẽ được liệt kê ở đây ...
Ví dụ giải thích - Cơ sở dữ liệu MySQL
Bảng cơ sở dữ liệu chúng tôi sử dụng trong ví dụ trên có vẻ như thế này:
1 | Peter | Griffin | 41 | Quahog | Nhà máy bia |
2 | Lois | Griffin | 40 | Quahog | Nhà máy bia |
3 | Lois | Newport | 39 | Quahog | Nhà máy bia |
4 | Lois | Newport | 41 | Quahog | Nhà máy bia |
Lois
Newport
Giáo viên piano
Joseph
Thí dụ
Thông tin người sẽ được liệt kê ở đây ...
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
Ví dụ giải thích - Cơ sở dữ liệu MySQL
Select a person:
Peter Griffin
Lois Griffin
Joseph Swanson
Glenn Quagmire
Bảng cơ sở dữ liệu chúng tôi sử dụng trong ví dụ trên có vẻ như thế này:
Tôi
Họ
Họ
- Tuổi tác
- Quê nhà
- Nghề nghiệp
- Peter
Griffin
Quahog
Nhà máy bia
Lois
table {
width: 100%;
border-collapse: collapse;
}
Newport
border: 1px solid black;
padding: 5px;
}
Giáo viên piano
Bảng cơ sở dữ liệu chúng tôi sử dụng trong ví dụ trên có vẻ như thế này:
$q = intval($_GET['q']);
Tôi
if (!$con) {
die('Could
not connect: ' . mysqli_error($con));
}
Họ
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
Họ
Firstname
Lastname
Age
Hometown
Job
while($row = mysqli_fetch_array($result)) {
echo "
echo "
echo "
echo "
echo "
echo "
echo "
}
echo "";
mysqli_close($con);
?>
Tuổi tác
- Quê nhà
- Nghề nghiệp
- Peter