dongxin9759 2013-04-16 09:40
浏览 63
已采纳

使用Cookie提交PHP后保留表单值

I am looking for a way to keep form values after submit with cookies (after going to page2 and going back to page_form). I am really trying but i need you help guys.

I tried this but it didn't work

<? php
if (isset($_POST['Infos_test']))
{
$expire = 8*3600; 
setcookie("Infos_test", $_POST['from']&|&$_POST['area_html'], time()+$expire);  
}
?>

.....

<?php
if (isset($_COOKIE['Infos_test']))
{
$Infos_test = explode("&|&", $_COOKIE['Infos_test']); 
}
?>

.....

<input type="text" name="from" style="width:350px"  value="<?php echo $Info_test[0]; ?>"/>

<textarea valign="top" name="area_html" style="width:350px; height:150px; resize:none;" /><?php echo $Info_test[1]; ?></textarea>
  • 写回答

3条回答 默认 最新

  • doudouwd2017 2013-04-16 09:43
    关注

    I prefer using SESSION variables before cookies. Here is an example code:

    On the form recieving page:

    session_start();
    $_SESSION['from'] = $_POST['from'];
    $_SESSION['area_html'] = $_POST['area_html'];
    

    Then on your other page:

    <?php session_start(); ?>
    <!-- here is your html header etc -->
    From: <?php echo $_SESSION['from']; ?><br>
    Area HTML: <?php echo $_SESSION['area_html']; ?>
    

    Note that the session_start(); part must be stated BEFORE any other HTML output.

    HTML syntax for form should read:

    <input type="text" name="from" style="width:350px" value="<?php echo $_SESSION['from']; ?>" />
    <textarea valign="top" name="area_html"><?php echo $_SESSION['area_html']; ?></textarea>
    

    Note the type of the INPUT tag and the change how values should be inserted into a TEXTAREA.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog