簡體   English   中英

PHP AJAX加載更多

[英]PHP AJAX Load More

嗨,我有一個腳本,當用戶單擊“加載更多”按鈕時,ajax將請求新內容並顯示給用戶,但是我有一個問題,如果所有內容均已加載,並且如果用戶單擊“加載更多”按鈕,則會導致錯誤並反復顯示多次加載更多按鈕。以下是我的代碼,需要知道如何解決此問題。 如果沒有要加載的內容,則需要禁用該按鈕。謝謝大家!

ajax_more.php

 <?php
include("config.php");


if(isSet($_POST['lastmsg']))
{
$lastmsg=$_POST['lastmsg'];
$result=mysql_query("select * from messages where mes_id<'$lastmsg' limit 3");
$count=mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$msg_id=$row['mes_id'];
$message=$row['msg'];
?>


<li>
<?php echo $message; ?>
</li>


<?php
}


?>

<div id="more<?php echo $msg_id; ?>" class="morebox">
<a href="#" id="<?php echo $msg_id; ?>" class="more">more</a>
</div>

<?php
}
?>

loadmore.php

<?php
include('config.php');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Twitter Style load more results.</title>
<link href="frame.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>

<script type="text/javascript">
$(function() {
//More Button
$('.more').live("click",function() 
{
var ID = $(this).attr("id");
if(ID)
{
$("#more"+ID).html('<img src="moreajax.gif" />');

$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID, 
cache: false,
success: function(html){
$("ol#updates").append(html);
$("#more"+ID).remove();
}
});
}
else
{
$(".morebox").html('The End');

}


return false;


});
});

</script>
<style>
body
{
font-family:Arial, 'Helvetica', sans-serif;
color:#000;
font-size:15px;

}
a { text-decoration:none; color:#0066CC}
a:hover { text-decoration:underline; color:#0066cc }
*
{ margin:0px; padding:0px }
ol.timeline
    { list-style:none}ol.timeline li{ position:relative;border-bottom:1px #dedede dashed; padding:8px; }ol.timeline li:first-child{}
    .morebox
    {
    font-weight:bold;
    color:#333333;
    text-align:center;
    border:solid 1px #333333;
    padding:8px;
    margin-top:8px;
    margin-bottom:8px;
    -moz-border-radius: 6px;-webkit-border-radius: 6px;
    }
    .morebox a{ color:#333333; text-decoration:none}
    .morebox a:hover{ color:#333333; text-decoration:none}
    #container{margin-left:60px; width:580px }

</style>
</head>

<body>
<div style="padding:4px; margin-bottom:10px; border-bottom:solid 1px #333333; "><h3>Tutorial Link <a href="http://9lessons.blogspot.com/2009/12/twitter-style-load-more-results-with.html">Click Here</a></h3></div>
<div id='container'>
<ol class="timeline" id="updates">
<?php
$sql=mysql_query("select * from messages  LIMIT 3");
while($row=mysql_fetch_array($sql))
{
$msg_id=$row['mes_id'];
$message=$row['msg'];
?>
<li>
<?php echo $message; ?>
</li>
<?php } ?>
</ol>
<div id="more<?php echo $msg_id; ?>" class="morebox">
<a href="#" class="more" id="<?php echo $msg_id; ?>">more</a>
</div>
</div>

</body>
</html>
<?php
include("config.php");

$count=0;
$done=false;
if(isSet($_POST['lastmsg']))
{
$lastmsg=$_POST['lastmsg'];
$result=mysql_query("select * from messages where mes_id<'$lastmsg' limit 3");
$check=mysql_result(mysql_query("select mes_id from messages ORDER BY mes_id ASC limit 1"));
$count=mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$msg_id=$row['mes_id'];
$message=$row['msg'];
if($row['mes_id']==$check){$done=true;}
$count++;
?>


<li>
<?php echo $message; ?>
</li>


<?php
}

if($count>0 && !$done){
?>

<div id="more<?php echo $msg_id; ?>" class="morebox">
<a href="#" id="<?php echo $msg_id; ?>" class="more">more</a>
</div>

<?php
}
}
?>

說明:您無條件輸出了更多鏈接。 進行更改后,腳本將在輸出更多鏈接之前檢查是否已從表中加載了0條以上的消息。 我還更新了它,以檢查當前批次是否為最后一批,如果是則不輸出more div。

試試這個代碼;

   $.ajax({
**$("#load_buton").attr("disabled","disabled");**
    type: "POST",
    url: "ajax_more.php",
    data: "lastmsg="+ ID, 
    cache: false,
    success: function(html){
    $("ol#updates").append(html);
    $("#more"+ID).remove();
**$("#load_buton").removeAttr("disabled");**
    });

在php部分中,如果返回的行大小計數大於0,則僅顯示More按鈕

邊緣情況:

  1. 如果您的數據庫表大小增加了n行,則每當您單擊More時,您將重復n條記錄。
  2. 如上所述,但是如果刪除記錄,則將丟失記錄

安全問題:

  1. 通過在最后一個消息發布字段中發送“ 0”;截斷消息;-”來進行SQL注入
  2. 跨站點腳本編寫-如果用戶可以使用HTML / JavaScript提交消息,則將在內容中返回它們而不會進行轉義。

在以上兩種情況下,都使用。 MySQL轉義字符串( http://php.net/manual/zh/function.mysql-real-escape-string.php ),或使用mysqli,並使用htmlentities( http://php.net/manual/zh/function .htmlentities.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM