如何使用 javascript 设置会话变量并通过 php 代码获取它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34536886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set a session var using javascript and get it via php code
提问by BKF
I have a javascript code like this :
我有一个这样的 javascript 代码:
<script type="text/javascript">
$('#editRole').on('show.bs.modal', function (e) {
$roleID = $(e.relatedTarget).attr('data-id');
// Here I want to set this $roleID in session may be like this :
Session['roleID'] = $roleID;
});
</script>
Then I want to get that $roleID in an other place using php code, may be like this :
然后我想使用 php 代码在其他地方获取 $roleID,可能是这样的:
<?php $roleID = Session::get('roleID'); //do something .... ?>
Thanks
谢谢
回答by Moppo
You can't set a server session variable directly from JS.
您不能直接从 JS 设置服务器会话变量。
To do that you can make an AJAX call to a PHP script passing the value you want to set, and set it server side:
为此,您可以对 PHP 脚本进行 AJAX 调用,传递您要设置的值,并将其设置为服务器端:
$('#editRole').on('show.bs.modal', function (e) {
$roleID = $(e.relatedTarget).attr('data-id');
//ajax call
$.ajax({
url: "set_session.php",
data: { role: $roleID }
});
});
set_session.php
set_session.php
//preliminary code
Session::put('roleID', $request->input('role') );
回答by Rohan Khude
Above answer and from other resources together helped me out to make example similar to situation like 'Setting the session using AJAX in laravel'.
以上答案和其他资源一起帮助我制作了类似于“在 laravel 中使用 AJAX 设置会话”等情况的示例。
Im posting simple example, which other users might found this helpful.
我发布了一个简单的例子,其他用户可能会觉得这很有帮助。
View - ajax_session.blade.php
查看 - ajax_session.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#submit').on('click',function(){
// show that something is loading
$('#response').html("<b>Loading response...</b>");
$.ajax({
type: 'POST',
url: '/set_session',
data: $("#userform").serialize()
})
.done(function(data){
// show the response
$('#response').html(data);
})
.fail(function() {
// just in case posting your form failed
alert( "Posting failed." );
});
// to prevent refreshing the whole page page
return false;
});
});
</script>
</head>
<body>
<form id="userform">
{{ csrf_field() }} <!--required - otherwise post will fail-->
<input type="text" id="uname" name="uname" required/>
<button type='submit' id="submit">Submit</button>
<div id='response'></div>
</form>
</body>
</html>
routes.php
路由文件
Route::get('session_form', function () {
return view('ajax_session');
});
Route::post('set_session', 'SessionController@createsession');
Route::get('allsession', 'SessionController@getsession');
Controller - sessionController.php
控制器 - sessionController.php
public function createsession(Request $request)
{
\Session::put('uname', $request->uname);
echo "session created";
}
public function getsession()
{
dd(\Session::get('uname'));
}
You can check this by running localhost:8000/session_form
. And you can also check session separately by localhost:8000/allsession
.
您可以通过运行来检查这一点localhost:8000/session_form
。您还可以通过localhost:8000/allsession
.
Hope this helps!!!
希望这可以帮助!!!