需要将值从javascript变量保存到php会话

时间:2016-04-16 10:55:21

标签: javascript php

   <div id="fbcontainer" style="margin:50px">

<fb:like href="https://www.facebook.com/PTP-574862402690345/?ref=bookmarks"  send="false" layout="box_count" width="450" show_faces="true" font="verdana"></fb:like>

 <strong id="login">

 <span id="login-mask"></span>

 <a id="fb-login" href="#" onclick="FB.login()">Please Login with Facebook first!</a>

</strong>

</div>


<div id="after"><input type="submit" id="submit" class="take-quiz-btn index-btn redglow" value="Start" />

</div>
<input type="text" id="loc" name="loc" value="<?php echo @$_SESSION['quiz'];?>">




        </div>                        
    </form>

想要在点击时保存php会话变量中的值,并在用户再次不喜欢显示和隐藏开始按钮时更改会话值。当用户喜欢并刷新页面按钮消失时,应避免这样做。

var app_id = 'appid';
local = document.getElementById('loc').value;


window.onload = function(){

if (local=="") {
$('#after').css({"display":"none"});
console.log("1");
console.log("local1="+local);


} 
else{
 $('#after').css({"display":"block"});
 console.log("2");
 console.log("local2="+local);


 }
 }

(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
 js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId="+app_id;
fjs.parentNode.insertBefore(js, fjs);
 }(document, 'script', 'facebook-jssdk'));





  window.fbAsyncInit = function() {
  FB.init({
  appId : app_id, // App ID
  status : true, // check login status
  cookie : true, // enable cookies to allow the server to access the session
   xfbml : true // parse XFBML

  });



 FB.Event.subscribe('edge.create',
 function(response) {$('#after').css({"display":"block"});
 //$('#local').val('assign');
 //<?php $_SESSION['quiz'] = "assign"; ?>
  local="assign";
  local = <?php $_SESSION['quiz']; ?>
   console.log("3");
   console.log("local3="+local);
  alert(local); 
  }
  );



  FB.Event.subscribe('edge.remove',
  function(response) {$('#after').css({"display":"none"});
  // $('#local').val('assign');
   // <?php $_SESSION['quiz'] = ""; ?>
    local="";
   local = <?php $_SESSION['quiz']; ?>
   console.log("4");
   console.log("local4="+local);
   alert(local);        

    }
   );



    FB.Event.subscribe('auth.login', function() {

    window.location.reload();

     });





    FB.getLoginStatus(function(response) {



    if (response.status === 'connected') {



     // user is logged in and has authenticated your app

    var uid = response.authResponse.userID;

    var accessToken = response.authResponse.accessToken;



    } else if (response.status === 'not_authorized') {



   // user is logged in but has not authenticated your app

    console.log('Logged in no auth');



  } else {




    $('#login').fadeIn();

    $('#login-mask').fadeIn();

    console.log('NOT Logged in');



    }

   });



 $('#login-mask').live('click',function() {

 $('#fb-login').effect('shake', { times:3 }, 300);

 });

 };

1 个答案:

答案 0 :(得分:1)

使用Ajax完成

FB.Event.subscribe('edge.create',
 function(response) {$('#after').css({"display":"block"});
 $.ajax({
   url:"ajax.php",
   type:"post",
   data:{"type":"change"},
   success:function(data){

       console.log("change="+data);
   }



  });

  }
 );


     FB.Event.subscribe('edge.remove',
    function(response) {$('#after').css({"display":"none"});

    $.ajax({
   url:"ajax.php",
   type:"post",
   data:{"type":"again"},
   success:function(data){

       console.log("again="+data);
   }
   });
 console.log("4");
  }
);

PHP: -      

  @$type = $_REQUEST['type'];


  if($type == "change"){


$_SESSION['quiz'] = "assign";

echo $_SESSION['quiz'];

 }


 if($type == "again"){


 $_SESSION['quiz'] = "";

 echo $_SESSION['quiz'];

}


 ?>