doulin3510 2016-10-08 16:47
浏览 71
已采纳

使用AJAX时为什么不更改$ _SESSION变量(PHP)?

I can not understand a simple problem. Is there a website where I need to add a change of currency on the page (RUR / EUR / USD) using AJAX. I think the best option — it's written in $_SESSION selected currency and use it later on my PHP code (for display price, etc).

My index.php file:

<?php 
  session_start(); 
  if (!isset($_SESSION['currency'])) $_SESSION['currency'] = 'rouble';
?>

<!-- Links -->
<a href="#" class="currency__change" data-currency="dollar">Dollar</a>
<a href="#" class="currency__change" data-currency="euro">Euro</a>
<a href="#" class="currency__change" data-currency="rouble">Rouble</a>

<!-- Add jQuery and script -->
<script src="jquery-2.2.4-min.js"></script>
<script src="ajax.js"></script>

Here my ajax.js file:

;(function($, window, document, undefined) {
  // On document ready.
  $(document).ready(function() {
    // Change currency.
    $('.currency__change').on('click', function(event) {
      // Prevent default.
      event.preventDefault();
      // Send AJAX request.
      $.ajax({
        type: 'POST',
        url: 'Ajax.php',
        data: { 'currency': $(this).data('currency') },
        success: function() {
          location.reload();
        },
        error: function() {
          alert('Error! Try again later.');
        }
      });
    });
  });
})(jQuery, window, document);

And now, my Ajax.php file:

<?php
  session_start();
  // Change currency.
  if ($_POST['currency']) {
    unset($_SESSION['currency']);
    $_SESSION['currency'] = $_POST['currency'];
  }
?>

Session start normally, I get session file on my server (with default value) with the right headers:

enter image description here

But if I click AJAX link — variable in $_SESSION['currency'] don't change.
Why?

  • 写回答

2条回答 默认 最新

  • douwei1930 2016-10-08 17:22
    关注

    I would suspect that your variable $(this).data('currency') is not being set, and your if ($_POST['currency']) check is not passing.

    Try sending just a string value in for if ($_POST['currency']) to test the theory.

    data: { 'currency': 'Dollar' },
    

    you could also put a console.log() in to see if anything is in the variable.

    console.log("Anything here?",$(this).data('currency'));
    

    Aditionally

    You could implement some php error logging to see what your incoming data looks like.

    <?php
      session_start();
      // Change currency.
    
      error_log(print_r($_POST,true));
    
      if ($_POST['currency']) {
        unset($_SESSION['currency']);
        $_SESSION['currency'] = $_POST['currency'];
      }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料