KJS
KJS

Reputation: 1214

Update a session array with jquery / ajax

I have a somewhat theoretical question this time:

The situation (on a PHP website):

  1. members on a website can add cd's to their 'favorite list'
  2. onClick of the like-button, jQuery/ajax ads the cd to the favorite list
  3. another file renews the session array [favCDS]
  4. goal: the user always has his latest clicks updated real time, because live-data is generated from stored arrays)

The question:

Would it be possible to update a SESSION array of personal member values with a function, through a file running on the background, called by and updated through jQuery/ajax?

I imagine it would be updating it all in one file, but I wonder if you guys have any thoughts/ideas on this.

Upvotes: 0

Views: 852

Answers (1)

Jordan
Jordan

Reputation: 4628

The PHP session persists even after scripts are finished running, so you don't need a PHP file running in the background on the server.

Example PHP file (called by the ajax function, assuming the ajax function submits a POST request with the CD's id):

update-favorite.php

$_SESSION['favCDS'][] = $_POST['cd-id'];

That way, when the user navigates to a new page, that page can preserve the favorite CDs by accessing this session array and generating HTML accordingly.

Upvotes: 1

Related Questions