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条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统