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 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄