我正在實施一個不定式滾動網頁。它運作良好。它有如何獲取ajax請求到同一頁面?
1. index.php
2. getrecords.php
兩頁。
我index.php
頁面
<html>
//some html codes here
//my java script
<script type="text/javascript">
var busy = false;
var limit = 6
var offset = 0;
var anotherID = 5
function displayRecords(lim, off) {
$.ajax({
type: "GET",
async: false,
url: "getrecords.php",
data: "limit=" + lim + "&offset="+ off+"&anotherID="+anotherID,
cache: false,
beforeSend: function() {
$("#loader_message").html("").hide();
$('#loader_image').show();
},
success: function(html) {
$("#results").append(html);
$('#loader_image').hide();
if (html == "") {
$("#loader_message").html('<button class="btn btn-default btn-block" type="button">No more records.</button>').show()
} else {
$("#loader_message").html('<button class="btn btn-default btn-block" type="button"><div id="loader_image"><img src="loader.gif" alt="" width="24" height="24">Loading please wait...</button>').show();
}
window.busy = false;
}
});
}
$(document).ready(function() {
// start to load the first set of data
if (busy == false) {
busy = true;
// start to load the first set of data
displayRecords(limit, offset);
}
$(window).scroll(function() {
// make sure u give the container id of the data to be loaded in.
if ($(window).scrollTop() + $(window).height() > $("#results").height() && !busy) {
busy = true;
offset = limit + offset;
// this is optional just to delay the loading of data
setTimeout(function() { displayRecords(limit, offset); }, 500);
// you can remove the above code and can use directly this function
// displayRecords(limit, offset);
}
});
});
</script>
</html>
我getrecords.php
頁面
<?php
require_once("config.php");
$limit = (intval($_GET['limit']) != 0) ? $_GET['limit'] : 10;
$offset = (intval($_GET['offset']) != 0) ? $_GET['offset'] : 0;
$id = $_GET["anotherID"];
$query = $id;
$sql = "SELECT * FROM x where title like '%xx%' ORDER BY rand() LIMIT $limit OFFSET $offset";
try {
$stmt = $DB->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll();
} catch (Exception $ex) {
echo $ex->getMessage();
}
if (count($results) > 0) {
foreach ($results as $res) {
echo 'something';
}
}
?>
出於某種原因,我想兩個頁面合併成一個頁面。我想將getrecords.php
的編碼放入index.php
的內部並製作一個頁面。我嘗試了很多選擇,但沒有任何幫助。我怎樣才能做到這一點?提前致謝。
是什麼你遇到的問題? –
當我在我的java腳本中輸入index.php而不是getrecords.php時,獲取url路徑並將整個get records.php代碼放在我的index.php中,但它不能幫助我。它會再次回聲整個index.php並使其崩潰 – Subi