【发布时间】:2016-07-14 11:48:46
【问题描述】:
我想在启动查询的按钮所在的页面上显示我的查询内容。
<form action="action.php" method="post">
<input type="submit" class="button" id="mail-button" name='msubmit' value="View Mail" />
此时按钮将我发送到包含以下代码的 action.php
//这是我的action.php
if (isset($_POST['mail-button'])) { // if the view message button is click
include('..\time.php');
include('..\db.php');
$con = mysqli_connect($dbsrvname, $dbusername, $dbpassword, $dbname);
$delete = mysqli_query($con,"DELETE FROM notopen WHERE username= '".$_COOKIE[$cookie_name]."' AND value='1'");
$q = mysqli_query($con,"SELECT * FROM tbl1 WHERE username = '".$_COOKIE[$cookie_name]."'");
while($row = mysqli_fetch_assoc($q)){
//echo $row['id'];
$id = $row['id'];
$username = $row['username'];
$q = mysqli_query($con,"SELECT * FROM inbox WHERE rcpuser= '".$_COOKIE[$cookie_name]."' AND senderid ORDER BY time DESC
LIMIT 20");
// I WOULD LIKE TO DISPLAY THESE RESULTS BACK ON THE SAME PAGE AS THE BUTTON WHILE USING AJAX TO AVOID PAGE REFRESHING
echo '<table border="1" width="500px">';
echo'<tr>';
echo '<table class="table table-fixed table-bordered table-hover">';
echo "<tr><th>From</th><th>Message</th><th>Time-Date</th></tr><tr>";
while($row = mysqli_fetch_assoc($q))
{
//echo $row['senderid']; // to reply back to sender
$senerid = $row['senderid'];
echo "<tr><td>";
echo "<a href=message.php?id=".$senerid.">Reply to : </a>";
echo $row['username']; // username of sender
echo "</td><td>";
echo $row['message'];
echo "</td><td>";
echo "time_passed"($row['time']); //time_passed calling funtion
echo "</td></tr>";
}
echo "</table>";
上面将结果显示在表格中以供查看,现在我想使用 AJAX,因此结果显示在同一页面上而无需重新加载,而不是按钮重定向到上述代码的操作。我需要帮助了解如何让我的表格在我的脚本上显示成功以及如何正确格式化 .下面是我的脚本
// I dont want to redirect here, I want my script to execute and have the results displayed back on this page
<form action="action.php" method="post">
<script src="/js/jquery.js"></script>
<script>
$('#mail-button').on('click', function() {
var info = $('#name').val();
$.ajax({
method: "POST",
url: "action_mail_view.php",
data: {
name: info
},
success: function(response){
$("#table_wrapper").html(response);
//alert(response);
}
});
});
</script>
<table border="1" align="center">
<tr>
<td> <input type="button" class="button" id="mail-button" name='name' value="View Mail" /> </td>
</tr>
</table>
<div id="table_wrapper"></div>
请问我该如何实现?只需单击按钮,即可执行查询 (action.php),并且不刷新 (AJAX) 并且表格的结果与按钮显示在同一页面上。提前致谢。
【问题讨论】:
-
向我们展示这个文件
about_me_action.php -
它在上面,我现在只有它包含查询和按钮单击后的 if isset
-
about_me_action.php在代码中重命名为action.php