将PHP会话与jquery一起使用 [英] Use PHP sessions with jquery
问题描述
嗯,我的问题很简单:
我希望在if语句中在jQuery中使用我的PHP会话,但是我不知道如何以一种简单的方式做到这一点.我应该以某种方式在jQuery中导入$_SESSION
吗?
I wish to use my PHP sessions in jQuery, in an if statement, however I do not know how I can do this on an easy way. Am I supposed to somehow import the $_SESSION
in jquery?
我基本上想做的是,如果User_ID与$_SESSION
User_ID相同,则打开一个对话框.我也希望检查一下该会话是否有人是管理员,商店经理或用户.此信息已存在于PHP中,而User_ID仅存在于jQuery中.
What I basically want to do, is open a dialog if the User_ID is the same as the $_SESSION
User_ID. Also I wish to have it checked to see if someone is an administrator, shop-manager or a user, with the session. This information already exists in PHP and the User_ID only exists in jQuery.
if($admin == true || gebr_id == $_SESSION['id']){
Javascript code here?
}
我认为这是解决问题的最简单方法,但是这似乎对我不起作用.也许我忘记了让PHP理解其javascript的标签?
I think that would be the easiest way to solve it, however that does not seem to work for me. Perhaps I am forgetting tags for the PHP to understand it's javascript?
我想出了一种解决方案的简便方法,谢谢大家!
I've figured out an easy way to come around the solution, thanks you you all!
解决方案:
$('.do').click(function(){
<?php
if ($admin == true) {
echo "$('#dialog').dialog('open');\n";
echo "var cid = $(this).attr('id');\n";
echo "var datum = cid.substr(0, 10);\n";
echo "var naampos = cid.indexOf('|');\n";
echo "var gebridpos = cid.indexOf('||');\n";
echo "var naam = cid.substring(naampos+1,gebridpos);\n";
echo "var gebrid = cid.substring(gebridpos+2);\n";
echo "$.ajax({\n";
echo "type: \"POST\",\n";
echo "url: \"agenda.php\",\n";
echo "data: naam,\n";
echo "success: function(){\n";
echo "$('#gebruikerinput').html(\"<input type='text' READONLY='' size='35' value='\" + naam +\"'>\");\n";
echo "$('#gebridinput').html(\"<input type='hidden' name='gebridtextbox' value='\" + gebrid + \"'>\");\n";
echo "$('#datuminput').html(\"<input type='text' READONLY='' size='12' name='datum' value='\" + datum + \"'>\");\n";
echo "}\n";
echo "})\n";
echo "return false;\n";
}
?>
});
到目前为止,它似乎一直在工作.我要做的唯一一件事就是让jQuery返回一个User_ID变量,该变量必须与会话ID相同.
It seems to be working so far. The only thing left for me to do is to make so jQuery returns a User_ID variable which needs to be identical to the session ID.
推荐答案
if($admin == true || gebr_id == $_SESSION['id']){
$javascript = "javascript code here";
}
,然后在您的html
and then in your html
<body>
<?php echo $javascript ?>
</body>
这篇关于将PHP会话与jquery一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!