如何在javascript文件中访问php会​​话? [英] How to access php session in javascript file?

查看:67
本文介绍了如何在javascript文件中访问php会​​话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码:

index.php文件

index.php file

index.php文件的javascript

javascript of index.php file

function Result()
{
  var marks = 55;
  document.getElementById("hdnmarks").innerHTML= marks; 
  window.location = "results.php";
}

index.php的HTML

HTML of index.php

<input type="hidden" name="hdnmarks" id="hdnmarks">

说明:我有一个网址为localhost/index.php的网页.在index.php中,我有一个提交按钮,单击该按钮时会调用Result javascript方法,该方法将标记设置为55,并将其放入隐藏字段中,并带我到results.php页面.

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.

results.php中,我必须在数据库中插入标记的值.但是我应该如何访问标记存储在index.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?

我想在会话中添加标记,但是如何在javascript函数中维护PHP会话?我的意思是,在移至results.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?

推荐答案

您可以像<?php session_start();?>这样在页面上启动会话,并像这样为会话创建隐藏字段

you can start session on your page like <?php session_start();?> and create hidden field for session like this

<input type="hidden" name="mysession" id="mysession">

并修改javascript函数之类的东西

and modify javascript function some thing like this

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

这篇关于如何在javascript文件中访问php会​​话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆