0

I have an event listener on an input that executes an ajax request to set PHP session variable.

This works on the first event trigger, but after that, the PHP session variable remains unchanged.

This is my PHP chunk

session_start();
if(isset($_GET['driver'])){ 
        $driver = $_GET['driver'];
        $_SESSION['driver'] = $driver;
}

This is my javascript

$('#save').change(function() {
$.ajax({
                url: 'index.php',
                method: 'GET',
                data: {
                    'driver': 'example'
                }
});

This is my console log. Starting with the first change trigger. I am using dump_session to output the contents of the php variables. Right before the "Attempting ajax request" would be the new modified inputs that are being sent in the ajax request.

builder.js:168 (2) [_.P, _.P]
builder.js:170 ["Galerías Toluca"]
builder.js:171 Attempting ajax request
builder.js:318 array(2) {
  ["destinos"]=>
  string(22) "["Galerías Toluca"]"
  ["markers"]=>
  string(82) "[{"lat":19.2889701,"lng":-99.61342109999998},{"lat":19.2898867,"lng":-99.6226059}]"
}

builder.js:168 (3) [_.P, _.P, _.P]
builder.js:170 (2) ["Galerías Toluca", "IEDIS TOLUCA"]
builder.js:171 Attempting ajax request
builder.js:318 array(2) {
  ["destinos"]=>
  string(22) "["Galerías Toluca"]"
  ["markers"]=>
  string(82) "[{"lat":19.2889701,"lng":-99.61342109999998},{"lat":19.2898867,"lng":-99.6226059}]"
}
3
  • Why would it if you send the same hard coded value each time? Did you want to send a different value each time and if so from what source? Commented Nov 17, 2018 at 15:25
  • I trimmed down my code for readability, these sessions vars will pass to the next page where there is no way to change them, the data from the event listener actually comes from Google Map locations, and markers. This is to be used by an app in my place of work, to control Driver's routes. Commented Nov 17, 2018 at 15:40
  • Ok but not enough shown to detail the problem Commented Nov 17, 2018 at 15:49

2 Answers 2

1

I assume that you already used unset($_SESSION['driver']); before update $_SESSION['driver'] = $driver; and the problem still the same

Sign up to request clarification or add additional context in comments.

1 Comment

I hadn't and it worked. Thank you very much sir. You answered my question.
1

try this code

require "src/akSession.php" ; 

$session= akSession::getInstance();

if(isset($_GET['driver'])){ 
        $driver = $_GET['driver'];
        $session->set('driver', $driver);
}

don't forget to add composer require aminelch/ak-session to your composer file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.