加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

通过单击更改php会话值

发布时间:2020-12-13 17:45:57 所属栏目:PHP教程 来源:网络整理
导读:我想通过点击为会话赋值 我试图这样做,但它不起作用: ?php session_start(); $_SESSION['role']="";?!DOCTYPE htmlhtml xmlns="http://www.w3.org/1999/xhtml" headtitle/titlelink href="auth-buttons.css" rel="stylesheet" /link href="StyleSheet.css"
我想通过点击为会话赋值

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

<?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'];

?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读