我如何将PHP变量传递给Ajax [英] How can i pass a php variable to ajax
问题描述
我有一个自动加载页面,我需要能够基于一个变量检索数据,因为该变量必须返回特定值.下面的代码基于检索所有数据.但是我只需要选择一些基于$ list
I have a auto load page and i need to be able to retrieve the data based on a variable as that variable must bring back a specific value. The code below is based on retrieving all the data.But i only need a select few which is based on the $list
Page.php
<?php
<div class="page-main ">
$query="SELECT * FROM page WHERE page_id='$list'";
$counting="SELECT * FROM page WHERE page_id='$list'";
$rows=mysqli_query($connection,$counting);
$rows_counts=mysqli_num_rows($rows);
$results=mysqli_query($connection,$query);
confirm_query($results);
?>
<div class="loader">
<img src="loader.gif" alt="loading gif"/>
</div>
</div> <!--close page main -->
这是传递给ajax的jquery(在同一页面上)
here is the jquery passing to ajax (it is on same page)
$(document).ready(function(){
$('.loader').hide();
var load=0;
$.post("ajax.php",{load:load},function(data){ // somehow i need to pass $list to here
$('.page-main').append(data);
}); // close ajax
$(window).scroll(function(){
if($(window).scrollTop() == $(document).height() - $(window).height())
{
$('.loader').show();
load++;
$.post("ajax.php",{load:load},function(data){
$('.page-main').append(data);
$('.loader').hide();
}); // close ajax
};
});// close window.scroll
});// close document.ready
这是ajax.php(现在在这里我得到未定义的变量$ list,我需要传递$ list,但我不确定如何将此$ list从php传递到jquery到ajax.
this is ajax.php ( now here i am getting undefined variable $list, i need to pass $list i am not sure how to pass this $list from php to jquery to ajax.
$load=htmlentities(strip_tags($_POST["load"])) * 6;
$query="SELECT * FROM page WHERE page_id='$list' ORDER BY page_id DESC LIMIT ".$load.",6";
$result=mysqli_query($connection,$query);
confirm_query($result);
//之后while循环
// after this while loop ect
推荐答案
尝试setting $list to a javascript variable
.像:
var list=<?php echo $list?>;
然后按照传递var load
的方式传递它.
then pass it the way you are passing var load
.
这篇关于我如何将PHP变量传递给Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!