在表中显示 MySQL 结果而无需重新加载页面 [英] Show MySQL Results in a table without reloading page
问题描述
我正在尝试编写一个用于数据库管理的 Web 应用程序.我已经在 HTML+CSS3 上编写了应用程序的前端,并且服务器端语言是 PHP,但我仍然对如何进行 MySQL 调用并在不刷新/重新加载页面的情况下向用户显示它感到困惑.
I'm trying to write a web application for database managing purposes. I've written front-end of the app on HTML+CSS3 and the server-side language is PHP, but I'm still confused about how to make MySQL calls and show it to the user without refreshing/reloading the page.
我想说得具体一点,以下是我的疑问:
Trying to be specific, here are my doubts:
- 我可以在不重新加载页面的情况下执行 MySQL 查询吗?
- 如何显示包含查询结果的表格?
推荐答案
为此使用 Ajax :
将此代码添加到要显示表格数据的主页
Add this code to main page where you want to display table data
<html>
<head>
<script>
function dashboard() {
var query_parameter = document.getElementById("name").value;
var dataString = 'parameter=' + query_parameter;
// AJAX code to execute query and get back to same page with table content without reloading the page.
$.ajax({
type: "POST",
url: "execute_query.php",
data: dataString,
cache: false,
success: function(html) {
// alert(dataString);
document.getElementById("table_content").innerHTML=html;
}
});
return false;
}
</script>
</head>
<body>
<div id="table_content"></div>
</body>
</html>
在 table_content div 中,来自 execute_query.php 页面的数据将在不刷新页面的情况下加载.
In table_content div the data come from execute_query.php page will load without refreshing the page.
execute_query.php
$user_name = $_POST['parameter'];
$query="SELECT * from info where name=$user_name";
$result=mysql_query($query);
$rs = mysql_fetch_array($result);
do
{
?>
<table>
<tr>
<td><?php echo $rs['city']; ?></td>
<td><?php echo $rs['phone_number']; ?></td>
</tr>
</table>
<?php
}while($rs = mysql_fetch_array($result));
这篇关于在表中显示 MySQL 结果而无需重新加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!