我试图计算一个SESSION变量,但我不希望用户看到任何刷新。
我对下面代码的问题是,它只更改了一个,然后需要刷新。我怎么能在没有任何刷新的情况下做到这一点?
test page:
<?php session_start(); ?>
<script src="../../../common/js/jquery-1.11.2.min.js"></script>
<script src="../../../common/js/jquery.touchwipe.min.js"></script>
<?php
if(empty($_SESSION['counter'])){
$_SESSION['counter'] = 1;
}
$count = $_SESSION['counter'];
?>
<div id="main"><?= $count; ?></div>
<button id="detailed">Link</button>
<script type="text/javascript">
$(document).ready(function(){
$(document).on('click','#detailed',function(){
var count = "<?= $count ?>";
count++;
$.ajax({
type: "POST",
url: "test.php",
data: {countertje: count},
success:function(data){
$('#main').html(data);
console.log(data);
}
});
})
});
</script>
do_ajax.php
<?php
session_start();
$_SESSION['counter'] = $_POST['countertje'];
echo $_SESSION['counter'];
?>
答案 0 :(得分:0)
你可以在javascript中使用的东西
var count = $('#main').val();
而不是以下:
var count = "<?= $count ?>";
在javascript中编写PHP代码不是一个好主意!
答案 1 :(得分:0)
<script type="text/javascript">
var count = "<?= $count ?>";
$(document).ready(function(){
$(document).on('click','#detailed',function(){
count++;
$.ajax({
type: "POST",
url: "test.php",
data: {countertje: count},
success:function(data){
$('#main').html(data);
console.log(data);
}
});
})
});
</script>
count
应该是一个全局变量