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 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突