简体   繁体   English

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

[英]Why don't change $_SESSION variables (PHP) when use AJAX?

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. 是否有一个网站需要使用AJAX在页面上添加货币更改(RUR / EUR / USD)。 I think the best option — it's written in $_SESSION selected currency and use it later on my PHP code (for display price, etc). 我认为最好的选择是-用$_SESSION所选货币编写,稍后在我的PHP代码中使用(用于显示价格等)。

My index.php file: 我的index.php文件:

<?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: 这是我的ajax.js文件:

;(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: 现在,我的Ajax.php文件:

<?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: 会话正常启动,我在服务器上获取带有正确标题的会话文件(具有默认值):

在此处输入图片说明

But if I click AJAX link — variable in $_SESSION['currency'] don't change. 但是,如果我单击AJAX链接,则$_SESSION['currency']变量不会更改。
Why? 为什么?

Session are saved on the server and not on the user browser. 会话保存在服务器上,而不保存在用户浏览器上。 To now who is the owner of the session saved session php set a cookie in the user browser called PHPSESSID with a unique id to identify that specific user. 现在,谁是会话保存会话的所有者php在用户浏览器中设置一个名为PHPSESSID的cookie,该cookie具有唯一的ID以标识该特定用户。 When you call the function session_start() php use PHPSESSID to load the saved session for that user. 当您调用函数session_start() php时,请使用PHPSESSID为该用户加载已保存的会话。

To check if session are working add print of the session in the index.php after set the default session. 要检查会话是否正常工作,请在设置默认会话后在index.php添加会话的打印。

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

<!-- Currency -->
Selected currency: <b><? echo $_SESSION['currency']; ?></b><br>

<!-- 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>

If you need to access to the currency from javascript than you can store it directly in the cookies or crate a javascript function that make a ajax call to php that return the stored currency. 如果您需要从javascript访问货币,则可以将其直接存储在cookie中,或创建一个javascript函数以对返回的ajax调用php,以返回存储的货币。

Here you can found how to store and read cookies in javascript: http://www.w3schools.com/js/js_cookies.asp 在这里,您可以找到如何在javascript中存储和读取cookie: http : //www.w3schools.com/js/js_cookies.asp

Here you can found more info on php session: http://php.net/manual/en/intro.session.php 在这里,您可以找到有关php会话的更多信息: http : //php.net/manual/en/intro.session.php

Some advice: You don't need to unset the session you can just overwrite it. 一些建议:您无需取消会话设置,只需覆盖它即可。 You can't use if($_POST['currency']) with all php version but fore sure you can do it from the version 5.6 您不能在所有php版本中使用if($_POST['currency']) ,但要确保可以从5.6版开始使用

I would suspect that your variable $(this).data('currency') is not being set, and your if ($_POST['currency']) check is not passing. 我怀疑您的变量$(this).data('currency')没有设置,并且您的if ($_POST['currency'])检查没有通过。

Try sending just a string value in for if ($_POST['currency']) to test the theory. 尝试仅在for if ($_POST['currency'])发送一个字符串值以测试该理论。

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

you could also put a console.log() in to see if anything is in the variable. 您还可以放入console.log()来查看变量中是否包含任何内容。

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

Aditionally 附加地

You could implement some php error logging to see what your incoming data looks like. 您可以实现一些php错误日志记录,以查看传入数据的外观。

<?php
  session_start();
  // Change currency.

  error_log(print_r($_POST,true));

  if ($_POST['currency']) {
    unset($_SESSION['currency']);
    $_SESSION['currency'] = $_POST['currency'];
  }
?>

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

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