SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Updating $_SESSION with jQuery AJAX

    Hi...

    I have a page (http://nerotic.net/aux/) that's using php includes to populate a <div>. At the same time I'm setting a session variable in the included file as such:

    PHP Code:
    <?php 
        $_SESSION
    ["nav"] = "how";
    ?>

    In the file that calls in the <div> I use the following to generate the nav:

    PHP Code:
    <? session_start();
    $section $_SESSION["nav"];
    ?>
    HTML Code:
    <a href="level.php?page=level-<?php echo $section;?>" class="tnav">
    The reason I'm doing this is because I want to be able to go across the nav without going to the default page for each section. For example: if I'm in Sound/Where and I click on any of the models on top (Level, Classic, Box) I should end up in either Level/Where, Classic/Where, or Box/Where.

    My left side nav looks like this:
    HTML Code:
    <a href="index.php?page=soundwhy" id="soundwhy"  class="lnav">why</a>
    <a href="index.php?page=soundhow" id="soundhow" class="lnav">how</a>
    <a href="index.php?page=soundwhat" id="soundwhat" class="lnav">what</a>
    <a href="index.php?page=soundwhere" id="soundwhere" class="lnav">where </a> 
    <a href="index.php?page=soundwho" id="soundwho" class="lnav">who</a>

    The problem is as follows:

    Because I'm not reloading the page at that very instance $section isn't updating, so the nav is always one click behind where the user actually is. (You can see this at the URL only in the Sound section though, I'm echoing both $id and $section around the word Sound. You'll need to click on any of Why/How/Where/What/Who *two* times to see the $section echo change.)

    I've spent time researching my options and have seen that I can take the cheap way out by using a frame or iframe but I'm not willing to take that route, just not willing to give up deep linking. So apparently my best option is to use jQuery AJAX to update that variable and that's where I stand now.

    I saw an example online that I've been trying to get to work but it's not and this is what I'm seeking help for.

    Based on the example I saw I've added the following AJAX to my page:


    Code:
    $("a.lnav").onclick(function()
    {
        // Get the ID of the link
        var src = $(this).attr("id");
    
        // Send Ajax request to backend.php, with src set as "img" in the POST data
        $.post("/backend.php", {"id": src});
    });
    and then have a backend.php file with the following code:

    PHP Code:
    <?php
        
    // do any authentication first, then add POST variable to session
        
    $_SESSION['nav'] = $_POST['id'];
    ?>
    I've tried echoing $id but it's not working.

    What step am I missing?

  2. #2
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •