javascript 如何在javascript文件中访问php会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19874064/
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 to access php session in javascript file?
提问by MSS
Below is my code:
下面是我的代码:
index.php file
index.php 文件
javascript of index.php file
index.php 文件的 javascript
function Result()
{
var marks = 55;
document.getElementById("hdnmarks").innerHTML= marks;
window.location = "results.php";
}
HTML of index.php
index.php 的 HTML
<input type="hidden" name="hdnmarks" id="hdnmarks">
Description:I have a web page with url localhost/index.php
. In index.php, I have a submit button on the click of which I call Result
javascript method which sets the marks = 55 and put it into the hidden field and takes me to the results.php
page.
描述:我有一个带有 url 的网页localhost/index.php
。在 index.php 中,我有一个提交按钮,单击它我调用Result
javascript 方法,该方法设置标记 = 55 并将其放入隐藏字段并将我带到results.php
页面。
In results.php
, I have to insert value of marks in the database. But how should I access the marks as those were stored in the hidden field of index.php
file?
在 中results.php
,我必须在数据库中插入标记值。但是我应该如何访问存储在index.php
文件隐藏字段中的标记?
I want to put marks in the session, but how should I maintain the PHP session in javascript function? I mean where and when should I put marks in the session before moving to results.php
?
我想在会话中添加标记,但是我应该如何在 javascript 函数中维护 PHP 会话?我的意思是在转移到 之前,我应该在何时何地在会话中打分results.php
?
回答by Ram Sharma
you can start session on your page like <?php session_start();?>
and create hidden field for session like this
您可以像这样在您的页面上启动会话<?php session_start();?>
并为会话创建隐藏字段
<input type="hidden" name="mysession" id="mysession">
and modify javascript function some thing like this
并修改javascript函数这样的事情
function Result(){
var marks = 55;
document.getElementById("mysession").innerHTML= <?php echo session_id();?>;
document.getElementById("hdnmarks").innerHTML= marks;
document.getElementById('Form').submit();
}
change the Form name with your form name
使用您的表单名称更改表单名称
回答by ManZzup
your question have two parts
你的问题有两部分
1)
1)
But how should I access the marks as those were stored in the hidden field of index.php file?
但是我应该如何访问存储在 index.php 文件隐藏字段中的标记?
the standard way is using a form
标准方法是使用表格
<form action="index.php" method=POST>
<input type="hidden" name="hdnmarks" id="hdnmarks">
</form>
submit that form using a button or javascript to POST data to index.php
使用按钮或 javascript 提交该表单以将数据发布到 index.php
in index.php
在 index.php 中
<?php
$marks = $_POST['hdnmarks'];
?>
2)
2)
I mean where and when should I put marks in the session before moving to results.php? you have to start session and make a session variable
我的意思是在转移到 results.php 之前,我应该在何时何地在会话中添加标记?你必须开始会话并创建一个会话变量
index.php
索引.php
<?php
session_start();
$marks = $_POST['hdnmarks'];
$_SESSION['marks'] = $marks;
?>
result.php
结果.php
<?php session_start() ?>
...
//javascript code
var marks = <?php echo $_SESSION['marks'] ?>
...
NOTE: this isnt a very good way of passing data from one to another nor a good way of passing data from php to javascript and if you are using a database, session has no use in this as well
注意:这不是将数据从一个传递到另一个的好方法,也不是将数据从 php 传递到 javascript 的好方法,如果您使用的是数据库,会话也没有用
回答by MSS
Just Javascript Use This:
只是 Javascript 使用这个:
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
getCookie('PHPSESSID');