down00112 2013-06-13 19:17
浏览 34
已采纳

使用会话保存文本框和单选按钮值

I have an exam and what I am looking to do use session to save every value, whether it's textboxes or selects or radio button groups choices. So if the user submits the form and misses something, coming back to the form from a different page, it keeps all the value that was entered and auto fills it in. I would also like to keep the session alive for 2 hours. I know I can use something of the following:

FORM PAGE:

<form method=post action="submit.php">
    <input type=text value="" name="first" />

    <input type=radio value="A" name="acct" /> A
    <input type=radio value="B" name="acct" /> B

    <input type=radio value="A" name="birt" /> A
    <input type=radio value="B" name="birt" /> B
</form>

On submission of the form in the submit.php page I have this:

    <?php
    session_start();
    $_SESSION['textbox1'] = isset($_POST[first]) ? $_POST[first] : null;
    $_SESSION['radiogroup1'] = isset($_POST[acct]) ? $_POST[acct] : null;
    $_SESSION['radiogroup2'] = isset($_POST[birt]) ? $_POST[birt] : null;

** PLUS OTHER CODES I USE **
    ?>

The above should start a session and save those values so no matter if I exit the page or not, it should store it. Correct?

Now questions I have are:

  • How do I make the session alive for ONLY 2 hours
  • Let's say I exit the page or go to another page and I come back to the FORM page, how do I auto enter the value which was originally saved in the session?
  • In WHAT SHOULD I ENTER HERE How do I accomplish, if the user selected a value fill it in otherwise leave it alone? SOLVED

I really appreciate the help.

  • 写回答

3条回答 默认 最新

  • dpd20130 2013-06-13 19:23
    关注

    1st answer:

    SESSIONS remain alive until the user closes his/her browser so you need to make a cookie like this:

    setcookie("TestCookie", $value, time()+(3600*2));  //save for 2 hours
    

    2nd answer:

    Use the array : $_COOKIE['session_name']

    3rd answer:

    Just use null there.

    Simple :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛