的search.php
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<input type="text" name="search" id="search" autocomplete="off">
<button type="submit" class="btn btn-primary">Search</button>
</form>
<?php $search = $_POST['search']; ?> // This is what I've tried.
<script type="text/javascript" src="js/search.js"></script>
我試着加入$search = $_POST['search'];
,放入search.js
這樣var searchTerm = "<?php echo $search; ?>";
search.js
var ajax_arry=[];
var ajax_index =0;
var sctp = 100;
$(function(){
$('#loading').show();
var searchTerm = "<?php echo $search; ?>"; // This is what I've tried.
$.ajax({
url:"scroll.php",
type:"POST",
data:"actionfunction=showData&page=1&search="+searchTerm, // This is what I've tried.
cache: false,
success: function(response){
$('#loading').hide();
$('#demoajax').html(response);
}
});
$(window).scroll(function(){
var height = $('#demoajax').height();
var scroll_top = $(this).scrollTop();
if(ajax_arry.length>0){
$('#loading').hide();
for(var i=0;i<ajax_arry.length;i++){
ajax_arry[i].abort();
}
}
var page = $('#demoajax').find('.nextpage').val();
var isload = $('#demoajax').find('.isload').val();
if ((($(window).scrollTop()+document.body.clientHeight)==$(window).height()) && isload=='true'){
$('#loading').show();
var ajaxreq = $.ajax({
url:"scroll.php",
type:"POST",
data:"actionfunction=showData&page="+page,
cache: false,
success: function(response){
$('#demoajax').find('.nextpage').remove();
$('#demoajax').find('.isload').remove();
$('#loading').hide();
$('#demoajax').append(response);
}
});
ajax_arry[ajax_index++]= ajaxreq;
}
return false;
if($(window).scrollTop() == $(window).height()) {
alert("bottom!");
}
});
});
但是當我得到search
from data:"actionfunction=showData&page=1&search="+searchTerm
on scroll.php
查詢它不起作用。
function showData($data,$con,$limit){
$page = $data['page'];
if($page==1){
$start = 0;
}
else{
$start = ($page-1)*$limit;
}
$name = $data['search'];
$sql = "SELECT * FROM product WHERE p_name LIKE '%$name'";
你不能寫''裏面的文件.js' PHP code' – Saty
與嘗試替換'?''到'並在你的php文件中包含js –
urfusion
對不起<?php $ search = $ _POST ['search'];'?>'這只是發佈時的錯誤。我編輯過。 –