通过单击更改php会话值

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了通过单击更改php会话值脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想通过点击为会话赋值

我试图这样做,但它不起作用:

<?PHP session_start(); 
$_SESSION['role']="";?>
<!DOCTYPE htML>
<html XMlns="http://www.w3.org/1999/xhtml">
   <head>
<tITle></title>
<link href="auth-buttons.css" rel="stylesheet" />
<link href="StyleSheet.css" rel="stylesheet" />
   </head>
<body>

<div id="wrap">
<div id="wrapHome">
<p><a class="BTn-auth btn-faceBook large" href="redirect.PHP" onclick="<?PHP $_SESSION['role']="facebook" ?>" > Sign in with <b>Facebook</b> </a></p>

<p><a class="btn-auth btn-twitter large" href="redirect.PHP" onclick="<?PHP $_SESSION['role']="twitter" ?>" > Sign in with <b>Twitter</b> </a></p>

<p><a class="btn-auth btn-GOOGLE large" href="redirect.PHP" onclick="<?PHP $_SESSION['role']="google" ?>" > Sign in with <b>Google</b> </a></p>
</div>
</div>
</body>
</html>

解决方法

‘onclick’不会触发PHP代码.它会触发javascript.您可以使用javascript对一个PHP页面进行AJAX调用,该页面又能够设置您的会话值(并且ajax可以帮助您在按钮单击时不刷新页面的情况下执行此操作.

#('.btn-auth btn-facebook large').click(function(){
// fire off the request to /redirect.PHP
request = $.ajax({
    url: "/redirect.PHP",type: "post",data: 'facebook'
});

// callback handler that will be called on success
request.done(function (response,textstatus,jqXHR){
    // LOG a message to the console
    console.log("Hooray,it worked!");
});

// callback handler that will be called on failure
request.fail(function (jqXHR,errorThrown){
    // log the error to the console
    console.error(
        "The following error occured: "+
        textStatus,errorThrown
    );
    });
});

在你的redirect.PHP

<?PHP

$_SESSION['role'] = $_POST['data'];

?>

脚本宝典总结

以上是脚本宝典为你收集整理的通过单击更改php会话值全部内容,希望文章能够帮你解决通过单击更改php会话值所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。