本文介绍了AJAX显示MySQL的内容一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的网页应该与MySQL表的内容随时更新。我犯了一个AJAX功能。但是当我点击刷新按钮,复制相同的结果。我想说明一个表生只有一次。 这里是我的AJAX功能。
//网页上传功能
$(#显示)。点击(函数(){
$阿贾克斯({//创建一个Ajax请求load_page.php
键入:POST,
网址:display_status.php
数据类型:文字,//期待的HTML bereturned
成功:函数(响应){
$(#响应)追加(响应)。
//警报(响应);
}
});
});
});
< / SCRIPT>
< UL ID =响应>
< / UL>
<输入类型=按钮ID =显示值=刷新页面/>
那么这是我的PHP页面(display_status.php)
$结果= $ dbc->查询(SELECT * FROM状态,其中STUDENT_ID ='$ stu_id');
//从add_delete_record表中的所有记录
而($行= $结果> FETCH_ASSOC())
{
回声'<李ID =项目_'$行[status_id。'>';
回声'< DIV CLASS =del_wrapper>< A HREF =#级=del_buttonID =德尔 - '$行[status_id。'>';
回声'< IMG SRC =图像/ icon_del.gif边界=0/>';
回声'< / A>< / DIV>';
回声$行[地位。< /李>';
解决方案
而不是追加,使用这样的:
VAR preV = $(#响应)文本()。
VAR解析度= preV +响应;
$(#响应)HTML(RES)。
my page should update with mysql table content always. i made a ajax function. but when i click refresh button, duplicating same result. i want to show one table raw only once. here is my ajax function.
//page upload function
$("#display").click(function() {
$.ajax({ //create an ajax request to load_page.php
type: "POST",
url: "display_status.php",
dataType: "text", //expect html to bereturned
success: function(response){
$("#responds").append(response);
//alert(response);
}
});
});
});
</script>
<ul id="responds">
</ul>
<input type="button" id="display" value="Refresh Page" />
then this is my php page (display_status.php)
$results = $dbc->query("SELECT * FROM status where student_id='$stu_id'");
//get all records from add_delete_record table
while($row = $results->fetch_assoc())
{
echo '<li id="item_'.$row["status_id"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["status_id"].'">';
echo '<img src="images/icon_del.gif" border="0" />';
echo '</a></div>';
echo $row["status"].'</li>';
解决方案
instead of append ,use this:
var prev=$("#responds").text();
var res=prev+response;
$("#responds").html(res);
这篇关于AJAX显示MySQL的内容一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!