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:
But if I click AJAX link — variable in $_SESSION['currency']
don't change.
Why?