简体   繁体   English

如何在javascript文件中访问php会​​话?

[英]How to access php session in javascript file?

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 . 说明:我有一个网址为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. 在index.php中,我有一个提交按钮,单击该按钮会调用Result javascript方法,该方法将标记设置为55,并将其放入隐藏字段中,并带我到results.php页面。

In results.php , I have to insert value of marks in the database. results.php ,我必须在数据库中插入标记的值。 But how should I access the marks as those were stored in the hidden field of index.php file? 但是我应该如何访问这些标记,因为它们存储在index.php文件的隐藏字段中?

I want to put marks in the session, but how should I maintain the PHP session in javascript function? 我想在会话中添加标记,但是如何在javascript函数中维护PHP会话? I mean where and when should I put marks in the session before moving to results.php ? 我的意思是,在移至results.php之前,应在何时何地在会话中添加标记?

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 用您的表单名称更改表单名称

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? 我的意思是,在移至results.php之前,应在何时何地在会话中添加标记? you have to start session and make a session variable 您必须开始会话并设置会话变量

index.php index.php

<?php
session_start();
$marks = $_POST['hdnmarks'];
$_SESSION['marks'] = $marks;
?>

result.php result.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的很好的方法,如果您使用的是数据库,则会话也无用

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM