使用javascript ajax和php设置和获取会话变量

时间:2015-03-30 06:22:33

标签: javascript php ajax variables session

我正在尝试设置和检索会话变量。对于每个我有javascript代码运行ajax来调用php函数。在我的javascript代码中,我想使用返回的会话字段。什么都行不通。有人可以查看我的代码并告诉我我做错了什么。感谢名单

设置会话变量的MY Ajax代码是:

 {$.ajax({
 type: "POST",
 url: "setsessionvar.php",
 data: option,
 success: function(){ alert("success")
 }
 });

它的PHP功能是:

 function setvar($option) {
 session_start();
 $_SESSION["temp"]=$option;
 }

用于检索会话变量的MY Ajax代码是:

 {$.ajax({
 type: "POST",
 url: "getsessionvar.php",
 data: option,
success: function(option){
}
});

它的PHP功能是:

 function getsessionvar()
 {
 return .$_SESSION['option']; 

 } 

我对php代码进行了更改,但是没有任何工作仍然有效。我在ajax的成功部分有警报。两个警报都没有执行。我也不确定我是否正在访问ajax代码中的会话变量值。我这次包含了我页面的全部代码。 set ajax位于名为foo的函数中。 get ajax在例程startup中。

<!DOCTYPE HTML>
<html><!-- InstanceBegin template="/Templates/Ricks Universe.dwt" codeOutsideHTMLIsLocked="false" -->
<head>

<!-- InstanceBeginEditable name="doctitle" -->  
<title>Fun and Games</title>
<!-- InstanceEndEditable --> 
<meta charset="utf-8">
<meta name="viewport" content="width=device-width  initial-scale 1.0 minimum-scale 1.0" />

<link rel="stylesheet" href="ricks.css" type="text/css" media="screen" title="Ricks Style Sheet"/>
<link rel="stylesheet" href="hh.css" type="text/css" media="handheld"/>
<link rel="stylesheet" media="print" type="text/css" href="print.css" />   



<!-- InstanceBeginEditable name="head" -->    
<style type="text/css">
#Games{display:inline;}
#Humor{display:none;}
</style>

<script type="text/javascript">

  function foo(option)
  {

    var n = option.search(".HTM"); // Check for external file to link to;

    if (n > -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 < 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;
}

</script>

<script type="text/javascript">

 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"); }

     } 

  </script>
<!-- InstanceEndEditable -->

<!-- InstanceParam name="home" type="boolean" value="false" --> 

  <script type="text/javascript">
    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%";

};

</script>              

<style type="text/css">
<!--

-->@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;
    }

</style><!--[if lte IE 7]>
<style>
.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 */
</style>
<![endif]--></head>


<!-- InstanceBeginEditable name="onload" -->
<body onload="startup()">
<script type="text/javascript">

var string ="#Games,#Humor"; //Load table for foo to match against
var divs = new Array();
  divs = string.split(","); 

</script>
<!-- InstanceEndEditable --> 


<div class="container" id="wrapper">

  <div class="header" id="header">

  <p class="clock"> 


            <iframe src="http://free.timeanddate.com/clock/i425m2zy/n179/tct/pct/ahl/avt/tt0/ta1" frameborder="0" width="378" height="19"             allowTransparency="true"></iframe></p><br/><br/>


            <!-- InstanceBeginEditable name="title" -->
            <p class="title">Fun and Games</p>
            <!-- InstanceEndEditable -->


    <!-- end .header --></div>

  <div class="sidebar1" id="menu">
    <ul class="nav">
<li><a href="MEETDOGS.HTM">Meet the dogs</a></li> 
<li><a href="DOGPAGE.HTML">Rick's Dog Page</a></li>   
<li><a href="ART.HTM">Art</a></li> 
<li><a href="PARKING.HTM">Automotive</a></li>
<li><a href="BABY.HTM">Babies</a></li>
<li><a href="BOUTIQUE.HTM">The Boutique</a></li>
<li><a href="CATLG.HTM">Catalogs</a></li>
<li><a href="CHARITY.HTM">Charities</a></li>
<li><a href="COMPUTE.HTM">Computing</a></li>
<li><a href="DAYCARE.HTM">Day Care</a></li>
<li><a href="DEALS.HTM">Deals and Coupons</a></li>
<li><a href="DEPT.HTM">Department Stores</a></li>
<li><a href="DOGCOMP.HTM">Dogs In Competition</a></li>
<li><a href="DRUGS.HTM">Drug Stores</a></li>
<li><a href="EDUCATE.HTM">Education</a></li>
<li><a href="ELECT.HTM">Electronics</a></li>
<li><a href="EMPLOY.HTM">Employment</a></li>
<li><a href="ENTERT.HTM">Entertainment</a></li>
<li><a href="MONEY.HTM">Financial</a></li>
<li><a href="FITNESS.HTM">Fitness</a></li>
<li><a href="FLOWERS.HTM">Flowers</a></li>
<li><a href="FOOD.HTM">Food</a></li>
<li><a href="FUNGAME.HTM">Fun and Games</a></li>
<li><a href="GARDEN.HTM">Gardening</a></li>
<li><a href="GOVMENT.HTM">Government</a></li>
<li><a href="CARDS.HTM">Greeting Cards</a></li> 
<li><a href="GROCERY.HTM">Groceries</a></li>
<li><a href="HEALTH.HTM">Health</a></li>
<li><a href="HOBBY.HTM">Hobbies</a></li> 
<li><a href="HARDWARE.HTM">Home Improvement</a></li>
<li><a href="INFO.HTM">Information</a></li>
<li><a href="INTERNET.HTM">Internet and More</a></li>
<li><a href="JEWELRY.HTM">Jewelry</a></li>
<li><a href="BOOKS.HTM">Literature</a></li>
<li><a href="MAPDIR.HTM">Maps and Directions</a></li>
<li><a href="RECORDS.HTM">Music and Video</a></li>
<li><a href="MOVIES.HTM">Movies</a></li>
<li><a href="NEWS.HTM">News Stand</a></li>
<li><a href="OFFICE.HTM">Office Supplies</a></li>
<li><a href="PEOPLENET.HTM">People</a></li>
<li><a href="PETS.HTM">Pets</a></li>
<li><a href="PHONE.HTM">Phones</a></li>
<li><a href="PHOTO.HTM">Photography</a></li>
<li><a href="POST.HTM">Post Office</a></li>
<li><a href="PRICES.HTM">Price Comparison</a></li>
<li><a href="REALEST.HTM">Real Estate</a></li>
<li><a href="SECURITY.HTM">Security</a></li>
<li><a href="SEARCHNET.HTM">Searches</a></li>
<li><a href="SENIOR.HTM">Senior Page</a></li>
<li><a href="SHOES.HTM">Shoe Store</a></li>
<li><a href="SHOPPING.HTM">Shopping Clubs</a></li>
<li><a href="SPORTING.HTM">Sports</a></li>
<li><a href="TOYS.HTM">Toys and Games</a></li>
<li><a href="TV.HTM">Television</a></li>
<li><a href="TRAVEL.HTM">Travel</a></li>
</ul>

     <!-- end .sidebar1 --></div>


  <div class="content" id="content">
  <p class="menulink" > <a onclick="displaymenu()" href="javascript:void(0);" >     
     <span>Menu</span></a></p>



  <script type="text/javascript">
  var head = '<br/><br/><h3 id = "titlex">';
  var title=document.title;
  var trail = '</h3>'
  var heading  = head + title + trail;
  document.write(heading);
  </script>



    <!-- InstanceBeginEditable name="content" -->
  <p><img  class="pagepics" style="width:250px;"src="Pictures/weimball.jpg" alt="weimball.jpg" /></p>        

        <div id="form" class="form">
      <form action="">
        <h5>Page Directory</h5>
        <select  id="StoreDirectory" name="StoreDirectory" onchange="foo(this.value)">
          <option value="#Games">Games</option>
          <option value="#Humor">Humor</option>
         </select>
      </form>
    </div>   


            <div class="d1">

   <div  <a id="Games"  name="Games"></a>        
  <h2>Games</h2>
  <ul>
    <li><a href="http://www.addictinggames.com/">Addicting Games</a></li>
    <li><a href="http://www.bestcrosswords.com/">BestCrosswords.com</a></li>
    <li><a href="http://www.bingozone.com">The Bingo Zone</a></li>
    <li><a href="http://blufr.com/">blufr</a>A trivia game challenging you to identify statements as true or false</li>
    <li><a href="http://www.ababasoft.com/flash_games/">Free Flash Online Games</a></li>
    <li><a href="http://www.freearcade.com/">FreeArcade.com</a></li>
    <li><a href="http://www.gamespot.com/">Gamespot</a></li>
    <li><a href="http://www.itsyourturn.com/"> It's Your Turn</a>Chess, Checkers, Backgammon and more</li>
    <li><a href="https://www.soe.com/">Ultimate Online Gaming</a></li>
    <li><a href="http://www.jigzone.com/">JigZone</a>Jigsaw Puzzle Paradise</li>
    <li><a href="http://www.play-free-online-games.net/">Play-Free-Online-Games.net</a>Multi Player Games on the Internet</li>
    <li><a href="http://www.puzzlechoice.com/">Puzzle Choice</a></li>
    <li><a href="http://www.puzzleconnection.com/">The Puzzle Connection</a></li>
    <li><a href="http://www.websudoku.com/">Web Sudoku</a></li>
     </ul>
  </div>

  <div <a id="Humor"  name="Humor"></a> 
  <h2>Humor</h2>
  <ul>
    <li><a href="http://www.break.com/">Break</a>Funny Pics, Hot Chicks, and Cool Flicks</li>
    <li><a href="http://dumbcriminalacts.com/">Dumb Criminal Acts!</a></li>
    <li><a href="http://www.darwinawards.com/">The Darwin Awards</a>Improvement of the human genome by honoring those who accidentally kill themselves in really stupid ways</li>
    <li><a href="http://www.thepayback.com/">The Pay Back</a>Get revenge with this collection of revenge ideas, pranks, and practical jokes</li>
    <li><a href="http://www.funnypracticaljokes.com/">Practical Jokes</a></li>
  </ul>
  </div>
            </div>


        <!-- InstanceEndEditable -->
    <!-- end .content --></div>


  <div class="footer" id="footer">







        <a class="sitemap" href="SiteMap.htm">Site map</a> &nbsp;&brvbar;&nbsp;<a href="index.html">&nbsp;Home</a >



    <!-- end .footer --></div>

  <!-- end .container --></div>


<div id="sitelock_shield_logo" class="fixed_btm" style="bottom:0;position:fixed;_position:absolute;right:0;"><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;" ><img alt="PCI Compliance and Malware Removal" title="SiteLock"  src="http://shield.sitelock.com/shield/www.rickzipser.com"/></a></div>


</body>
<!-- InstanceEnd --></html>              

1 个答案:

答案 0 :(得分:0)

更改此行

return .$_SESSION['option'];

return $_SESSION['option'];