的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
data:"actionfunction=showData&page=1&search="+searchTerm
获取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'";
答案 0 :(得分:2)
你应该在你的php文件中包含你的Js代码。因为你不能在js文件中编写php代码。而js代码可以用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 if ($_POST) {
$search = $_POST['search'];
?>
<script>
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!");
}
});
});
</script>
<?php } ?>
答案 1 :(得分:0)
解释
你没有在PHP中“存储”PHP变量。 PHP是服务器端,javascript在客户端工作。他们必须沟通。
<强>解决方案强>
使用php创建隐藏输入,您可以使用javascript
{PersonId: "9245fe4a-d402-451c-b9ed-9c1a04247482", Firstname: "Jackie", Lastname: "Chan", DateOfBirth: "1968-05-17T00:00:00", CreateDate: "2015-12-11T09:15:49.403"…}
CreateDate: "2015-12-11T09:15:49.403"
DateOfBirth: "1968-05-17T00:00:00"
Firstname: "Jackie"
Lastname: "Chan"
ModifyDate: "2015-12-11T09:15:49.403"
PersonId: "9245fe4a-d402-451c-b9ed-9c1a04247482"
使用<input type="hidden" name="Language" value="English" id="val1">
在javascript中调用php文件,让php文件返回所需的值
在将提供给用户的PHP文件中使用嵌入式javascript。但是我不建议这样做。