注意:请不要重复我的问题。
我正试图在按下“加载更多”按钮时获取新记录。
我在这里面临的问题是我无法改变 $Limit
以及 $offset
因此,相同的记录被一次又一次地获取。
html代码:
<body>
<nav>
<h1 id="h1Nav">Myheading</h1>
<ul>
<li><a onclick="window.location('Home.html');" href="">Home</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Info</a></li>
</ul>
</nav>
<section id="contentSection">
</section>
<button name="LOAD_MORE_REQUEST" value="loadMoreButton" onclick="loadContent();" id="LoadMoreButton">Load more
</button>
<section id="footerSection">
<form action="">
<p>Enter You email adress and get notified:-</p>
<input type="email" id="footerEmailInput" name="footerEmailInputField" placeholder="Please enter yor email...">
<input type="submit" id="submitFooterEmailInputFieldSubmitButton" name="submitFooterEmailInputField">
</form>
<div id="linksDivFooterSection">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Privacy</a></li>
<li><a href="#">Events</a></li>
<li><a href="#">Contacts</a></li>
</ul>
</div>
</section>
<script src="Home.js" type="text/javascript"></script>
</body>
</html>
js代码:
function loadContent(){
var loadMoreButton = document.getElementById("LoadMoreButton").value;
var sendLoadContentRequest = new XMLHttpRequest();
requestQuery_Data = new FormData();
requestQuery_Data.append("LOAD_MORE_REQUEST", loadMoreButton);
sendLoadContentRequest.open("POST", "LoadContent.php", true);
sendLoadContentRequest.send(requestQuery_Data);
DefaultText = document.getElementById("contentSection").innerHTML;
sendLoadContentRequest.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("contentSection").innerHTML = DefaultText + sendLoadContentRequest.responseText;
}
}
}
window.onload = loadContent();
以下是我的php代码:
require("connection.php");
ini_set('display_errors', 'On'); //to get errors displayed
error_reporting(E_ALL); // to kep errors on
if(isset($_POST["LOAD_MORE_REQUEST"])){
loadContentOnPage();
}
else{
echo("Error");
}
function loadContentOnPage()
{
global $offset;
global $Limit;
if(isset($_POST['Offset_Field_Request'])) {
echo($offset);
echo($Limit);
$offset = $offset + 2;
$Limit = $Limit + 0;
}
global $Preview_img;
global $myconnection;
global $Tag1;
global $Tag2;
global $Tag3;
global $Tag4;
$retriveQuery_Article = "SELECT ArticleTitle, PreviewText, PreviewImage FROM Articles ORDER BY ID DESC LIMIT $Limit, $offset";
$query_run = mysqli_query($myconnection, $retriveQuery_Article);
if(!$query_run){
echo("Error.... " . mysqli_error($myconnection));
}
while($result_Article = mysqli_fetch_assoc($query_run)) {
$Article_Title = $result_Article['ArticleTitle'];
$Article_PreviewText = $result_Article['PreviewText'];
$Article_PreviewImg = $result_Article['PreviewImage'];
$retriveQuery_Img = "SELECT imagePath, tag1, tag2, tag3, tag4 FROM image WHERE name='$Article_PreviewImg'";
$query_run_img = mysqli_query($myconnection, $retriveQuery_Img);
if(!$query_run_img){ echo("Again error.... " . mysqli_error($myconnection)); }
while($result_img = mysqli_fetch_assoc($query_run_img)){
$Tag1 = $result_img['tag1'];
$Tag2 = $result_img['tag2'];
$Tag3 = $result_img['tag3'];
$Tag4 = $result_img['tag4'];
}
$response = '
<section class="ArticleSection1">
<div class="imageDivArticleSection1">
<img src="' . $Preview_img . '"></img>
</div>
<div class="PreviewTextDiv">
<h1>' . $Article_Title . '</h1>
<br>
<p> '
. $Article_PreviewText .
'</p>
<br>
<button>Read more</button>
<span class="TagSpan"><a class="TagLink"> Dev ' . $Tag1 . '</a></span>
<span class="TagSpan"><a class="TagLink"> Dev ' . $Tag2 . '</a></span>
<span class="TagSpan"><a class="TagLink"> Dev ' . $Tag3 . '</a></span>
<span class="TagSpan"><a class="TagLink"> Dev ' . $Tag4 . '</a></span>
</div>
</section>
';
echo($response);
}
}
1条答案
按热度按时间dxxyhpgq1#
抱歉,我没有看到代码中的行,您正在尝试重写变量$offset。
如果您正在发送获取新行的ajax请求,则必须发送post变量以检查偏移量。
e、 x您的请求可能包含get&page=2
“page”-加载的页面数,您将其保存在用户端的js中。
在jsu的用户端必须有变量'var page=1',当用户按下按钮'more'时,您必须使用get参数'page'发送ajax请求,并将变量'var page'增加1;
在你的服务器端的php你得到url参数'页'和乘以你的限制,所以你会得到你的偏移量。
我没有写下所有的观点,但我希望它能帮助你。