我正在实施一个不定式滚动网页。它运作良好。它有如何获取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