Using Javascript to Dynamically Update PHP Session Variables

Discussion in 'PHP' started by audax, Dec 11, 2006.

  1. #1
    I've just got a question about updating PHP sessions without reloading or redirecting to a separate page.

    For instance, say you have a catalogue of car models that you wanted to pick and choose your favorites from. While searching through the online catalog, you find a car that you're interested in. With the script that I'm looking for, you would be able to simply click "Add to Favorites" or something and it would automatically save it to a session variable favorites and then you could go to a separate "favorites" page that lists all the car models that you chose as your favorite. When you add a car to your favorites, the link that you'd press would just change automatically to "Remove from Favorites" or something without having to reload the page.

    Does that kind of make sense? I don't have any code snippet to show since I'm basically just looking for how to do it in the first place. I assume it'd be Javascript that I'd need to use in order to change the variable seeing as how PHP only runs once when a page is loaded. Unless I'm mistaken.
     
    audax, Dec 11, 2006 IP
  2. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could use AJAX (javascript) like on iStockPhoto when you add something to your lightbox (favorites). It pops over the page and runs a request to the backend servers without reloading where you are. You could also do it the old fashioned way with a tiny invisible iframe. Javascript sends an src='' command to the iframe, which loads a php page adding the necessary db rows.
     
    TheHoff, Dec 11, 2006 IP
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here's how I do pretty much the exact same:

    The list of items I display has some JavaScript links. onClick an AJAX event happens. It executes a file like favourites.php?session_id=kj67986dsv76sdv&ip=123.456.789 The php file stores the favourites list in MySQL and references, without the user having to log in or anything liek that, by IP and session_id. No iFrames, no direct writing to session vars (which you could do - just open the session file in tmp directly and write to it).
     
    T0PS3O, Dec 11, 2006 IP
  4. audax

    audax Peon

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What would that javascript/AJAX code look like for the onclick event?
    And maybe how it would look if there were an iframe as well. Just so I can get some ideas.
     
    audax, Dec 11, 2006 IP
  5. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
  6. audax

    audax Peon

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ah, I think I found kind of an example:

    http://www.rapha.cc/index.php?page=24

    Where it says "Buy Online" on the mid right-hand side, select something then click "Add." It automatically adds it without reloading.

    EDIT: What exactly would I be looking for under those results, T0PS30?
     
    audax, Dec 11, 2006 IP
  7. audax

    audax Peon

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I've decided to go with the iFrame method instead of AJAX (I'm not familiar with AJAX whatsoever and I couldn't exactly find what I was looking for in the way of tutorials, unless someone can point me to some good AJAX tutorials) I'm just bumping this to ask:

    How would I go about using an iFrame to do this? Or AJAX? What would the code look like?
     
    audax, Dec 13, 2006 IP
  8. Krownet

    Krownet Peon

    Messages:
    42
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Krownet, Dec 13, 2006 IP
  9. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #9
    With an iFrame, all you need to do is setup a small/invisible iFrame somewhere on the page. 1 x 1 pixels in size and then set the CSS display to none. Now within that iFrame, you load your database controls. Whenever the user clicks a certain button, you change the SRC in the iFrame so that it loads a script on the server updating the database.
     
    TheHoff, Dec 17, 2006 IP
  10. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #10
    Barti1987, Dec 17, 2006 IP
  11. audax

    audax Peon

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Ah ok. Right on. Thanks!

    I'm also going to keep looking into doing it the AJAX way, simply for some useful extra knowledge.

    Thanks TheHoff and azizny.
     
    audax, Dec 19, 2006 IP