title: setting and getting session variables using javascript ajax and php
body: <p>I am trying to set and retrieve session variables.  For each I have javascript code that runs ajax to call php functions. In my javascript code I want to use the returned session field.   Nothing works. Can someone look at my code and tell me what I did wrong.  Thanx</p>
  
  <p>MY Ajax code to set the session variable is:</p>
  
  <pre><code> {$.ajax({
   type: "POST",
   url: "setsessionvar.php",
   data: option,
   success: function(){ alert("success")
   }
   });
  </code></pre>
  
  <p>Its PHP function is:</p>
  
  <pre><code> function setvar($option) {
   session_start();
   $_SESSION["temp"]=$option;
   }
  </code></pre>
  
  <p>MY Ajax code to retrieve the session variable is:</p>
  
  <pre><code> {$.ajax({
   type: "POST",
   url: "getsessionvar.php",
   data: option,
  success: function(option){
  }
  });
  </code></pre>
  
  <p>It's PHP function is:</p>
  
  <pre><code> function getsessionvar()
   {
   return .$_SESSION['option']; 
  
   } 
  </code></pre>
  
  <p>I made the changes to the php code, however nothing is still working. I have an alert in the success sections of the ajax.  Neither alert  executes.  I am also not sure I am accessing the session variable value in the ajax code.  I am including the entire code of my page this time.  The set ajax is in the function called foo.  The get ajax is in the routine called startup.</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;&lt;!-- InstanceBegin template="/Templates/Ricks Universe.dwt" codeOutsideHTMLIsLocked="false" --&gt;
  &lt;head&gt;
  
  &lt;!-- InstanceBeginEditable name="doctitle" --&gt;  
  &lt;title&gt;Fun and Games&lt;/title&gt;
  &lt;!-- InstanceEndEditable --&gt; 
  &lt;meta charset="utf-8"&gt;
  &lt;meta name="viewport" content="width=device-width  initial-scale 1.0 minimum-scale 1.0" /&gt;
  
  &lt;link rel="stylesheet" href="ricks.css" type="text/css" media="screen" title="Ricks Style Sheet"/&gt;
  &lt;link rel="stylesheet" href="hh.css" type="text/css" media="handheld"/&gt;
  &lt;link rel="stylesheet" media="print" type="text/css" href="print.css" /&gt;   
  
  
  
  &lt;!-- InstanceBeginEditable name="head" --&gt;    
  &lt;style type="text/css"&gt;
  #Games{display:inline;}
  #Humor{display:none;}
  &lt;/style&gt;
  
  &lt;script type="text/javascript"&gt;
  
    function foo(option)
    {
  
      var n = option.search(".HTM"); // Check for external file to link to;
  
      if (n &gt; -1)  // If true...
      {
  
      window.location.href=option;
      }
  
      for (i in divs)   //loop thru divs table
      {
      x=divs[i].substr(1) //Get value for option without #
      z=document.getElementById(x) // Locate div
  
      if (option == divs[i])  //On match to table...
      {$.ajax({
  type: "POST",
  url: "setsessionvar.php",
  data: option,
  success: function(){ alert("success")
  }
  });
  
  
          createCookie("fungame", option,1);
          z.style.display= "inline";
      }
       else
          {
      z.style.display="none"    //On no match hide div on page
      }
    }
    }
  function createCookie(name, value, days) {
      var expires;
  
      if (days) {
  
          var date = new Date();
          date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
          expires = "; expires=" + date.toGMTString();
      } else {
  
          expires = "";
      }
     document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
      //document.cookie = escape(name) + "=" + escape(value) + "; path=/";
  }
  
  
  function readCookie(name) {
      var nameEQ = escape(name) + "=";
      var cookies = document.cookie.split(';');
      for (var i = 0; i &lt; cookies.length; i++) {
          var cookie = cookies[i];
          while (cookie.charAt(0) === ' ')
              cookie = cookie.substring(1, cookie.length);
  
          if (cookie.indexOf(nameEQ) === 0)
              return unescape(cookie.substring(nameEQ.length, cookie.length));
      }
      return null;
  }
  
  &lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
  
   function startup()
    {
  
   //document.cookie = opt + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
  
      if (option == divs[i])  //On match to table...
      {$.ajax({
  type: "POST",
  url: "getsessionvar.php",
  data: option,
  success: function(option){
      alert(option);
  }
  });
  
  
  
   var 
   option = readCookie("fungame");
  
    if (option != null) { foo(option);
  
   document.getElementById("StoreDirectory").value = readCookie("fungame"); }
  
       } 
  
    &lt;/script&gt;
  &lt;!-- InstanceEndEditable --&gt;
  
  &lt;!-- InstanceParam name="home" type="boolean" value="false" --&gt; 
  
    &lt;script type="text/javascript"&gt;
      function displaymenu() {
  
      document.getElementById("content").style.display="none";
      document.getElementById("header").style.display="none";
      document.getElementById("footer").style.display="none";
      document.getElementById("menu").style.display ='inline';
      document.getElementById("menu").style.width="100%"; 
      document.getElementById("menu").style["text-align"]='center';
      document.getElementById("menu").style.height="300%";
      document.getElementById("menu").style.zoom="300%";
  
  };
  
  &lt;/script&gt;              
  
  &lt;style type="text/css"&gt;
  &lt;!--
  
  --&gt;@media only screen 
      and (max-device-width : 667px) 
      and (orientation : portrait) 
      and (-webkit-min-device-pixel-ratio : 2)
  
      {
          .sidebar1 {display:none;}
          .header {display:none;}
          .footer {display: none;}
          .pagepics {display:none;}
          .menulink {display:inline;}
          .content {height:100%;
                    width:100%;}
          .textSmall{
              font-size:x-large;
              font-weight:bold;
      }
      #titlex {display:inline;}
      .bigpic {
          width:400;
          margin-left:0px;
      }
  
  &lt;/style&gt;&lt;!--[if lte IE 7]&gt;
  &lt;style&gt;
  .content { margin-right: -1px; } /* this 1px negative margin can be placed on any of the columns in this layout with the same corrective effect. */
  ul.nav a { zoom: 1; }  /* the zoom property gives IE the hasLayout trigger it needs to correct extra whiltespace between the links */
  &lt;/style&gt;
  &lt;![endif]--&gt;&lt;/head&gt;
  
  
  &lt;!-- InstanceBeginEditable name="onload" --&gt;
  &lt;body onload="startup()"&gt;
  &lt;script type="text/javascript"&gt;
  
  var string ="#Games,#Humor"; //Load table for foo to match against
  var divs = new Array();
    divs = string.split(","); 
  
  &lt;/script&gt;
  &lt;!-- InstanceEndEditable --&gt; 
  
  
  &lt;div class="container" id="wrapper"&gt;
  
    &lt;div class="header" id="header"&gt;
  
    &lt;p class="clock"&gt; 
  
  
              &lt;iframe src="http://free.timeanddate.com/clock/i425m2zy/n179/tct/pct/ahl/avt/tt0/ta1" frameborder="0" width="378" height="19"             allowTransparency="true"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;br/&gt;&lt;br/&gt;
  
  
              &lt;!-- InstanceBeginEditable name="title" --&gt;
              &lt;p class="title"&gt;Fun and Games&lt;/p&gt;
              &lt;!-- InstanceEndEditable --&gt;
  
  
      &lt;!-- end .header --&gt;&lt;/div&gt;
  
    &lt;div class="sidebar1" id="menu"&gt;
      &lt;ul class="nav"&gt;
  &lt;li&gt;&lt;a href="MEETDOGS.HTM"&gt;Meet the dogs&lt;/a&gt;&lt;/li&gt; 
  &lt;li&gt;&lt;a href="DOGPAGE.HTML"&gt;Rick's Dog Page&lt;/a&gt;&lt;/li&gt;   
  &lt;li&gt;&lt;a href="ART.HTM"&gt;Art&lt;/a&gt;&lt;/li&gt; 
  &lt;li&gt;&lt;a href="PARKING.HTM"&gt;Automotive&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="BABY.HTM"&gt;Babies&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="BOUTIQUE.HTM"&gt;The Boutique&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="CATLG.HTM"&gt;Catalogs&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="CHARITY.HTM"&gt;Charities&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="COMPUTE.HTM"&gt;Computing&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="DAYCARE.HTM"&gt;Day Care&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="DEALS.HTM"&gt;Deals and Coupons&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="DEPT.HTM"&gt;Department Stores&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="DOGCOMP.HTM"&gt;Dogs In Competition&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="DRUGS.HTM"&gt;Drug Stores&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="EDUCATE.HTM"&gt;Education&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="ELECT.HTM"&gt;Electronics&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="EMPLOY.HTM"&gt;Employment&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="ENTERT.HTM"&gt;Entertainment&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="MONEY.HTM"&gt;Financial&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="FITNESS.HTM"&gt;Fitness&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="FLOWERS.HTM"&gt;Flowers&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="FOOD.HTM"&gt;Food&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="FUNGAME.HTM"&gt;Fun and Games&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="GARDEN.HTM"&gt;Gardening&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="GOVMENT.HTM"&gt;Government&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="CARDS.HTM"&gt;Greeting Cards&lt;/a&gt;&lt;/li&gt; 
  &lt;li&gt;&lt;a href="GROCERY.HTM"&gt;Groceries&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="HEALTH.HTM"&gt;Health&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="HOBBY.HTM"&gt;Hobbies&lt;/a&gt;&lt;/li&gt; 
  &lt;li&gt;&lt;a href="HARDWARE.HTM"&gt;Home Improvement&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="INFO.HTM"&gt;Information&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="INTERNET.HTM"&gt;Internet and More&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="JEWELRY.HTM"&gt;Jewelry&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="BOOKS.HTM"&gt;Literature&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="MAPDIR.HTM"&gt;Maps and Directions&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="RECORDS.HTM"&gt;Music and Video&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="MOVIES.HTM"&gt;Movies&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="NEWS.HTM"&gt;News Stand&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="OFFICE.HTM"&gt;Office Supplies&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="PEOPLENET.HTM"&gt;People&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="PETS.HTM"&gt;Pets&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="PHONE.HTM"&gt;Phones&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="PHOTO.HTM"&gt;Photography&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="POST.HTM"&gt;Post Office&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="PRICES.HTM"&gt;Price Comparison&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="REALEST.HTM"&gt;Real Estate&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="SECURITY.HTM"&gt;Security&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="SEARCHNET.HTM"&gt;Searches&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="SENIOR.HTM"&gt;Senior Page&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="SHOES.HTM"&gt;Shoe Store&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="SHOPPING.HTM"&gt;Shopping Clubs&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="SPORTING.HTM"&gt;Sports&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="TOYS.HTM"&gt;Toys and Games&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="TV.HTM"&gt;Television&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="TRAVEL.HTM"&gt;Travel&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  
       &lt;!-- end .sidebar1 --&gt;&lt;/div&gt;
  
  
    &lt;div class="content" id="content"&gt;
    &lt;p class="menulink" &gt; &lt;a onclick="displaymenu()" href="javascript:void(0);" &gt;     
       &lt;span&gt;Menu&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
  
  
  
    &lt;script type="text/javascript"&gt;
    var head = '&lt;br/&gt;&lt;br/&gt;&lt;h3 id = "titlex"&gt;';
    var title=document.title;
    var trail = '&lt;/h3&gt;'
    var heading  = head + title + trail;
    document.write(heading);
    &lt;/script&gt;
  
  
  
      &lt;!-- InstanceBeginEditable name="content" --&gt;
    &lt;p&gt;&lt;img  class="pagepics" style="width:250px;"src="Pictures/weimball.jpg" alt="weimball.jpg" /&gt;&lt;/p&gt;        
  
          &lt;div id="form" class="form"&gt;
        &lt;form action=""&gt;
          &lt;h5&gt;Page Directory&lt;/h5&gt;
          &lt;select  id="StoreDirectory" name="StoreDirectory" onchange="foo(this.value)"&gt;
            &lt;option value="#Games"&gt;Games&lt;/option&gt;
            &lt;option value="#Humor"&gt;Humor&lt;/option&gt;
           &lt;/select&gt;
        &lt;/form&gt;
      &lt;/div&gt;   
  
  
              &lt;div class="d1"&gt;
  
     &lt;div  &lt;a id="Games"  name="Games"&gt;&lt;/a&gt;        
    &lt;h2&gt;Games&lt;/h2&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href="http://www.addictinggames.com/"&gt;Addicting Games&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.bestcrosswords.com/"&gt;BestCrosswords.com&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.bingozone.com"&gt;The Bingo Zone&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://blufr.com/"&gt;blufr&lt;/a&gt;A trivia game challenging you to identify statements as true or false&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.ababasoft.com/flash_games/"&gt;Free Flash Online Games&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.freearcade.com/"&gt;FreeArcade.com&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.gamespot.com/"&gt;Gamespot&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.itsyourturn.com/"&gt; It's Your Turn&lt;/a&gt;Chess, Checkers, Backgammon and more&lt;/li&gt;
      &lt;li&gt;&lt;a href="https://www.soe.com/"&gt;Ultimate Online Gaming&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.jigzone.com/"&gt;JigZone&lt;/a&gt;Jigsaw Puzzle Paradise&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.play-free-online-games.net/"&gt;Play-Free-Online-Games.net&lt;/a&gt;Multi Player Games on the Internet&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.puzzlechoice.com/"&gt;Puzzle Choice&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.puzzleconnection.com/"&gt;The Puzzle Connection&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.websudoku.com/"&gt;Web Sudoku&lt;/a&gt;&lt;/li&gt;
       &lt;/ul&gt;
    &lt;/div&gt;
  
    &lt;div &lt;a id="Humor"  name="Humor"&gt;&lt;/a&gt; 
    &lt;h2&gt;Humor&lt;/h2&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href="http://www.break.com/"&gt;Break&lt;/a&gt;Funny Pics, Hot Chicks, and Cool Flicks&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://dumbcriminalacts.com/"&gt;Dumb Criminal Acts!&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.darwinawards.com/"&gt;The Darwin Awards&lt;/a&gt;Improvement of the human genome by honoring those who accidentally kill themselves in really stupid ways&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.thepayback.com/"&gt;The Pay Back&lt;/a&gt;Get revenge with this collection of revenge ideas, pranks, and practical jokes&lt;/li&gt;
      &lt;li&gt;&lt;a href="http://www.funnypracticaljokes.com/"&gt;Practical Jokes&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/div&gt;
              &lt;/div&gt;
  
  
          &lt;!-- InstanceEndEditable --&gt;
      &lt;!-- end .content --&gt;&lt;/div&gt;
  
  
    &lt;div class="footer" id="footer"&gt;
  
  
  
  
  
  
  
          &lt;a class="sitemap" href="SiteMap.htm"&gt;Site map&lt;/a&gt; &amp;nbsp;&amp;brvbar;&amp;nbsp;&lt;a href="index.html"&gt;&amp;nbsp;Home&lt;/a &gt;
  
  
  
      &lt;!-- end .footer --&gt;&lt;/div&gt;
  
    &lt;!-- end .container --&gt;&lt;/div&gt;
  
  
  &lt;div id="sitelock_shield_logo" class="fixed_btm" style="bottom:0;position:fixed;_position:absolute;right:0;"&gt;&lt;a href="https://www.sitelock.com/verify.php?site=www.rickzipser.com" onclick="window.open('https://www.sitelock.com/verify.php?site=www.rickzipser.com','SiteLock','width=600,height=600,left=160,top=170');return false;" &gt;&lt;img alt="PCI Compliance and Malware Removal" title="SiteLock"  src="http://shield.sitelock.com/shield/www.rickzipser.com"/&gt;&lt;/a&gt;&lt;/div&gt;
  
  
  &lt;/body&gt;
  &lt;!-- InstanceEnd --&gt;&lt;/html&gt;              
  </code></pre>