简体   繁体   English

使用AJAX / PHP无法多次增加会话变量

[英]Cannot increment session variable more than once using AJAX/PHP

I have tried several ways to try and update this session variable more than once (ie to make it increment for EACH click rather than just the first one), but it is not working. 我尝试了几种尝试多次更新该会话变量的方法(即,使每次单击而不是第一次单击都增加),但是它不起作用。

<script>
$(document).ready(function(){
    $('.nextWeek').on('click', function(){
        $.post ('incrementWeek.php', function (response) {
            location.reload();
        });

        location.reload();
    });
});
</script>

I'm trying to increment the $_SESSION['idn'] variable for reuse after makes a click on a certain button/link. 我试图增加$ _SESSION ['idn']变量,以便在单击某个按钮/链接后再次使用。 "incrementWeek.php" is simply: “ incrementWeek.php”很简单:

<?php
    session_start();
    $session = $_SESSION['idn'];
    $session++;
    $_SESSION['idn'] = $session;
?>

Few quick answers to common questions: session_start() is the first thing under the first PHP tag, the $_SESSION['idn'] variable is set earlier in the file, but I use an if structure like this: 几个常见问题的快速答案很少:session_start()是第一个PHP标记下的第一件事,$ _SESSION ['idn']变量在文件中进行了较早的设置,但是我使用了如下if结构:

if (isset($_SESSION['idn'])) {

}

else {
    $id = getIDFromDate($connection, $currentSunday);
    $_SESSION['idn'] = $id;
}

I tried using an alert to see if the variable changes, but it stayed the same. 我尝试使用警报来查看变量是否更改,但它保持不变。 After the first page load, AJAX (I suppose?) does not even acknowledge that I am clicking the "nextWeek" class button. 加载第一页后,AJAX(我想是?)甚至都不承认我在单击“ nextWeek”类按钮。 The page does not reload meaning location.reload() was not executed. 该页面未重新加载,意味着location.reload()未执行。

Any ideas are appreciated. 任何想法表示赞赏。

EDIT: Thank you. 编辑:谢谢。 I have edited the code above from everyone's feedback and now it does not reload the page at all and the $_SESSION variable does not update. 我已经从每个人的反馈中编辑了上面的代码,现在它根本不会重新加载页面,并且$ _SESSION变量不会更新。 Is there something wrong syntactically? 语法上有问题吗?

$_SESSION['idn']++ is not correct. $_SESSION['idn']++不正确。 You should do this instead: 您应该这样做:

$session = intval($_SESSION['idn']);
$session++ ;
$_SESSION['idn'] = $session;

Setting session variable requires redirection after setting the value in it. 设置会话变量需要在它的值设置后重定向。

By this logic , becuase you are setting the session using the ajax call, you will not be able to redirect on server side to any url and hence you session variable is not getting set up. 按照这种逻辑,因为您正在使用ajax调用设置会话,所以您将无法在服务器端重定向到任何url,因此不会设置会话变量。

I think your need is to reuse the link clicked count somewhere for some purpose, what you can do here is as an alternate solution is to set your click count in the same ajax server side php file ie incrementWeek.php, but not using the session instead try using the cookie in php 我认为您的需要是出于某些目的在某处重用链接点击数,您可以在此处作为替代解决方案,在同一ajax服务器端php文件(即increasureWeek.php)中设置点击数,但不使用会话而是尝试在PHP中使用Cookie

Your code should be: 您的代码应为:

<script>
$(document).ready(function(){
    $('.nextWeek').on('click', function(){
        $.post ('incrementWeek.php', function (response) {
            location.reload();
        });

       // location.reload(); // <-- remove this as it is not required
    });
});
</script>

And

<?php
   session_start();
   if(isset($_SESSION['idn']))
   {
     $_SESSION['idn'] = $_SESSION['idn'] + 1;
   }
?>

TO use php inside javascript, you need to open php 要在javascript中使用php,您需要打开php

 <script>
  $(document).ready(function(){
    $('.nextWeek').on('click', function(){
    // dont do this 
     <?php  $_SESSION['idn']++; ?>
     // instead use a ajax 
      $.ajax({
    url: '/path/to/file',
    type: 'GET',
    data: {"session_increment":1},
    success:function(response){
        if(response==1)
        {
            location.reload();
        }
       }
     });
    });
   });

this will work, and will increment the value 这将起作用,并将增加值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM