PHP
$sortBy = array('upload_date','wall_views','wall_downloads');
$sort = 'upload_date';
if(isset($_GET['sort']) && in_array($_GET['sort'], $sortBy)){
$sort = $_GET['sort'];
}
ajax脚本
<script type="text/javascript">
var per_page = <?php echo $per_page;?>; //$per_page = 3
var last_page = <?php echo $last_page;?>; //$last_page = 4
var sort = <?php echo $sort;?>; //$sort = upload_date
$(function(){
$('.more').live('click', function(){
var page = $(this).attr('id'); //get the last id
$.ajax({
type: 'GET',
url: 'pagination.php',
data: {page: page, per_page: per_page, last_page: last_page, sort: sort},
success: function(data){
$('#itemContainer').append(data);
}
}); //ajax code end
}); //live end
}); //function end - when DOM is ready.
</script>
当我尝试将字符串(upload_date
)变量传递给ajax时出现问题,我不确定我是否正确传递字符串,如果只传入整数/数字,此代码可以正常工作代码。我在这里做错了什么?请帮忙。
答案 0 :(得分:4)
您必须在字符串变量周围添加引号。否则你生成的javascript看起来像变量名而不是字符串文字。
像这样:
var sort = "<?php echo $sort;?>"; //$sort = upload_date