使用Cookie提交PHP后保留表单值 [英] Keep form values after submit PHP with cookies

查看:388
本文介绍了使用Cookie提交PHP后保留表单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式来保存表单值提交后与cookies(在去page2后,回到page_form)。



我尝试了但是没有工作

 < ;? 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 =textname =fromstyle =width:350pxvalue =<?php echo $ Info_test [0];?>/> 

< textarea valign =topname =area_htmlstyle =width:350px; height:150px; resize:none; /><?php echo $ Info_test [1]; ?>< / textarea>


解决方案

我更喜欢在cookie之前使用SESSION变量。以下是示例代码:



在表单接收页面上:

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

然后在您的其他页面上:

 <?php session_start(); ?> 
<! - 这里是你的html头等等 - >
From:<?php echo $ _SESSION ['from']; >< br>
区域HTML:<??php echo $ _SESSION ['area_html']; ?>

注意 session_start();



表单的HTML语法应为:

 < input type =textname =fromstyle =width:350pxvalue =<?php echo $ _SESSION ['from'];?& /> 
< textarea valign =topname =area_html><?php echo $ _SESSION ['area_html']; ?>< / textarea>

请注意INPUT标签的类型以及如何将值插入TEXTAREA的更改。 p>

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>

解决方案

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.

这篇关于使用Cookie提交PHP后保留表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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