注意:请不要将我的问题标记为重复。
我试图在按下“加载更多”按钮时获取新记录。
我在这里面临的问题是我无法更改$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);
}
}
答案 0 :(得分:0)
对不起,但是我没有在您的代码中看到该行,您试图在其中重写变量$ offset。
如果您正在发送ajax请求以获取新行,则不建议发送post变量以检查偏移量。
e.x您的请求可能包含GET&page = 2
'PAGE'-已加载的页面数,您要在用户端保存在JS中。
在JS的用户端,您必须具有变量'var page = 1',并且当用户按下按钮'MORE'时,您必须发送GET参数为'page'的AJAX请求,并将变量'var page'加1。 ;
在服务器端通过php,您将获得url参数“页面”,并将其乘以您的限制,这样您就可以获得偏移量。
我没有写下所有要点,但我希望它能对您有所帮助。