将表单数据存储为会话变量 [英] Storing Form Data as a Session Variable

查看:162
本文介绍了将表单数据存储为会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想知道是否可以将来自表单的数据存储为会话变量。



这是我迄今为止的内容,但我不知道要为表单操作放置什么。



感谢您的期待!

 < strong>测试表格< / strong> 
< form action =methodpost>
< input type =textname =picturenum/>
< input type =submitname =Submitvalue =Submit! />
< / form>

<?
if(isset($ _ POST ['Submit'])){
$ _session ['picturenum'] = $ _POST ['picturenum'];
}
?>

< strong>< ;? echo $ _session ['picturenum'];?>< / strong>


解决方案

要使用会话变量,使用 session_start 函数,这将允许您以持久的方式将数据存储在全局变量 $ _ SESSION 中。



所以你的代码最终会看起来像这样:

 <强大>测试表格< / strong> 
< form action =methodpost>
< input type =textname =picturenum/>
< input type =submitname =Submitvalue =Submit! />
< / form>

<?php

//开始会话
session_start();


if(isset($ _ POST ['Submit'])){
$ _SESSION ['picturenum'] = $ _POST ['picturenum'];
}
?>

< strong><?php echo $ _SESSION ['picturenum'];?>< / strong>

为了方便使用并避免再次遗忘,您可以创建一个 session_file.php 它将包含在您的所有代码中,并将为您开始会话


$ b

session_start.php

strong>

 <?php 
session_start();
?>

然后将其包含在任何您喜欢的位置:

 < strong>测试表格< / strong> 
< form action =methodpost>
< input type =textname =picturenum/>
< input type =submitname =Submitvalue =Submit! />
< / form>

<?php

//包括会话文件
require_once(session_start.php);


if(isset($ _ POST ['Submit'])){
$ _SESSION ['picturenum'] = $ _POST ['picturenum'];
}
?>

这是将来可以轻松维护的便捷方式。



其他评论

如果您使用的是Apache版本2或更多,请小心,而不是

<?


打开php的标签,使用
<?php ,否则您的代码将不会被解释 PHP中的变量名称区分大小写,而不是写入$ _session,用大写字母写$ _SESSION


良好的工作!


So I was wondering if it would be possible to store data coming in from a form as a session variable.

Heres what I have so far, but I don't know what to put for the Form Action.

Thanks for looking!

<strong>Test Form</strong>
<form action="" method"post">
    <input type="text" name="picturenum"/>
    <input type="submit" name="Submit" value="Submit!" />
</form>

<? 
    if (isset($_POST['Submit'])) { 
        $_session['picturenum'] = $_POST['picturenum'];
    } 
?> 

<strong><? echo $_session['picturenum'];?></strong>

解决方案

To use session variables it's necessary to start the session by using the session_start function, this will allowed you to store your data in the global variable $_SESSION in a persistent way.

so your code will finally looks like this :

<strong>Test Form</strong>
<form action="" method"post">
<input type="text" name="picturenum"/>
<input type="submit" name="Submit" value="Submit!" />
</form>

<?php 

 // starting the session
 session_start();


 if (isset($_POST['Submit'])) { 
 $_SESSION['picturenum'] = $_POST['picturenum'];
 } 
?> 

<strong><?php echo $_SESSION['picturenum'];?></strong>

to make it easy to use and to avoid forgetting it again, you can create a session_file.php which will be include in all your codes and will start the session for you

session_start.php

 <?php
   session_start();
 ?> 

and then include it wherever you like :

<strong>Test Form</strong>
<form action="" method"post">
<input type="text" name="picturenum"/>
<input type="submit" name="Submit" value="Submit!" />
</form>

<?php 

 // including the session file
 require_once("session_start.php");


 if (isset($_POST['Submit'])) { 
 $_SESSION['picturenum'] = $_POST['picturenum'];
 } 
?> 

that's is the more portable and easy way to maintain in the future.

other remarks

  • if you are using Apache version 2 or more, be carefull instead of
    <?
    to open php's tags, use <?php, otherwise your code will not be interpreted

  • variables names in php are case-sensitives instead of write $_session, write $_SESSION in capital letters

good work !

这篇关于将表单数据存储为会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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