使用PHP Session存储表单数据 [英] Storing form data with PHP Session
问题描述
我们正在使用一个PHP Session来存储这些数据,并在需要时将元素发回。
问题在于它不起作用。我们需要在发生错误提交或页面刷新时保留表单数据。目前失败的提交或页面刷新中,没有数据存储在会话中。
我对PHP相当陌生,并且大多数代码不是我的,所以对我很容易。
使用的PHP Sumbit代码是:
软件:PHPMailer - PHP电子邮件类
版本:5.0.2
联系人:通过sourceforge.net支持页面(也是www.codeworxtech.com)
信息:http:/ /phpmailer.sourceforge.net
支持:http://sourceforge.net/projects/phpmailer/
SESSION:
<?php
session_name(fancyform);
session_start();
$ str ='';
if($ _ SESSION ['errStr'])
{
$ str ='< div class =error>'。$ _ SESSION ['errStr']。'< / DIV>';
unset($ _ SESSION ['errStr']);
}
$ success ='';
if($ _ SESSION ['sent'])
{
$ success ='< div class =message-sent>< p>消息已成功发送。谢谢! < / P>< / DIV>;
$ css ='< style type =text / css>#contact-form {display:none;}< / style>';
unset($ _ SESSION ['sent']);
}
?>
表格:
< form id =contact-formname =contact-formmethod =postaction =submit.php>
< p>< input type =textname =nameid =nameclass =validate [required]placeholder =在这里输入您的名字和姓氏value =< ?= $ _ SESSION [ '后'] [ '名称']>? />< / p为H.
< p>< input type =textname =emailid =emailclass =validate [required,custom [email]]placeholder =在此处输入您的电子邮件地址 < = $ _ SESSION [ '后'] [ '电子邮件'] GT;? />< / p为H.
< p>< input type =textname =phoneid =phoneplaceholder =在这里输入您的电话号码value =<?= $ _ SESSION ['post'] [ '电话']>? />< / p为H.
< p>
< select name =subjectid =subject>
< option>您主要关注哪些事件服务?< / option>
< option>事件创建< / option>
< option>项目管理< / option>
< option> Promotion< / option>
< / select>
< / p>
< input type =submitvalue =/>
< / form>
您正在输出 $ _ SESSION ['post'] ['form_element']
到你的模板中,但是在上面的PHP代码中没有提到设置这些数据。为了达到这个目的,你必须遍历$ _POST数组并将每个密钥对分配给$ _SESSION ['post']
在这一点上,你应该能够可以像上面编码一样访问以前提交的表单数据。
将其添加到您的submit.php中:
session_start();
foreach($ _POST AS $ key => $ value){
$ _SESSION ['post'] [$ key] = $ value;
}
这会将所有数据从$ _POST移动到$ _SESSION ['post' ]以供将来在模板中使用,并且应该是您需要的所有工具。
I am working on an HTML form whose data would be saved on a failed submit, or page refresh.
We are using a PHP Session to store this data, and we post the elements back when needed.
The issue is that it's not working. We need the form data to be preserved on a submit with errors, or page refresh. Currently on a failed submit, or page refresh, there is no data being stored in the session.
I'm fairly new to PHP, and most of this code is not mine, so go easy on me.
The PHP Sumbit code being used is:
Software: PHPMailer - PHP email class
Version: 5.0.2
Contact: via sourceforge.net support pages (also www.codeworxtech.com)
Info: http://phpmailer.sourceforge.net
Support: http://sourceforge.net/projects/phpmailer/
SESSION:
<?php
session_name("fancyform");
session_start();
$str='';
if($_SESSION['errStr'])
{
$str='<div class="error">'.$_SESSION['errStr'].'</div>';
unset($_SESSION['errStr']);
}
$success='';
if($_SESSION['sent'])
{
$success='<div class="message-sent"><p>Message was sent successfully. Thank you! </p></div>';
$css='<style type="text/css">#contact-form{display:none;}</style>';
unset($_SESSION['sent']);
}
?>
FORM:
<form id="contact-form" name="contact-form" method="post" action="submit.php">
<p><input type="text" name="name" id="name" class="validate[required]" placeholder="Enter your first and last name here" value="<?=$_SESSION['post']['name']?>" /></p>
<p><input type="text" name="email" id="email" class="validate[required,custom[email]]" placeholder="Enter your email address here" value="<?=$_SESSION['post']['email']?>" /></p>
<p><input type="text" name="phone" id="phone" placeholder="Enter your phone number here" value="<?=$_SESSION['post']['phone']?>" /></p>
<p>
<select name="subject" id="subject">
<option>What event service are you primarily interested in?</option>
<option>Event Creation</option>
<option>Project Management</option>
<option>Promotion</option>
</select>
</p>
<textarea name="message" id="message" class="validate[required]" placeholder="Please include some details about your project or event..."><?=$_SESSION['post']['message']?> </textarea>
<input type="submit" value="" />
</form>
You are outputting $_SESSION['post']['form_element']
to your template but in the above PHP code there is no mention of setting that data. For this to work, you would have to loop through the $_POST array and assign each key pair to $_SESSION['post']
At that point you should be able to access the previously submitted form data from the session just as you have coded above.
add this to your submit.php:
session_start();
foreach ($_POST AS $key => $value) {
$_SESSION['post'][$key] = $value;
}
This will move all the data from $_POST to $_SESSION['post'] for future use in the template, and should be all you need to get it working.
这篇关于使用PHP Session存储表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!