使用PHP Session存储表单数据


Storing form data with PHP Session

我正在处理一个HTML表单,该表单的数据将在提交失败或页面刷新时保存。

我们使用PHP会话来存储这些数据,并在需要时将元素发回。

问题是它不工作。我们需要在提交错误或页面刷新时保留表单数据。当前在失败的提交或页面刷新中,会话中没有存储数据。

我对PHP相当陌生,而且大部分代码都不是我写的,所以请原谅我。

使用的PHP Sumbit代码是:

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/  
会话:

<?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 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>

您正在将$_SESSION['post']['form_element']输出到模板中,但是在上面的PHP代码中没有提到设置该数据。为此,必须循环遍历$_POST数组,并将每个密钥对分配给$_SESSION['post']

此时,您应该能够从会话中访问先前提交的表单数据,就像您在上面编写的那样。

添加到您的submit.php:

session_start();    
foreach ($_POST AS $key => $value) {
 $_SESSION['post'][$key] = $value;
} 

这将把所有的数据从$_POST移动到$_SESSION['post'],以便将来在模板中使用,并且应该是所有你需要让它工作。

HTTP是无状态的,未提交的数据将不会保留,除非你使用客户端方法(webstorage等)。

您需要解析post变量并将它们添加到会话超全局变量中,现在您引用的是$_SESSION['post']['phone'],这不会像您期望的那样工作。

// Assuming same file, this is session section
if (array_key_exists('submit', $_REQUEST)) {
    if (array_key_exists('phone', $_REQUEST)) {
        $_SESSION['phone'] = $_REQUEST['phone'];
    }
}
// Form section
<?php $phone = (array_key_exists('phone', $_SESSION)) ? $_SESSION['phone'] : null;?>
<input type="text" name="phone" id="phone" placeholder="Enter your phone number here" value="<?php echo $phone ?>" />

或者您没有包含submit.php的代码,或者如果包含了,问题很明显:您从未设置会话值。为了做到这一点,你可以使用

session_start();
// etc...
$_SESSION['post']['name']  = $_POST['name'];
$_SESSION['post']['email'] = $_POST['email'];
$_SESSION['post']['phone'] = $_POST['phone'];
// etc...

submit.php上,然后表单页(大概是另一个页面?)可以检查这些值是否已设置

.. value="<?php if (isset($_SESSION['post']['name'])) echo $_SESSION['post']['name']; ?>" ..

我可能是错的,但我认为你需要通过使用类似

的东西从表单中获得张贴值
if ($_POST['errStr']) {
     $_SESSION['errStr'] = $POST['errStr'];
} 

如果我是对的,它的方式你试图访问变量后张贴的形式

如果您查看它设置为post的表单的METHOD属性,那么它应该返回您想要跨页面传递的值。

也许这不是你要问的,我有点不清楚问题是什么部分,我假设它从表单中获取值,并在下一页获取它们。

如果您想在页面刷新/退出时执行此操作,您可能需要使用一些javascript客户端来尝试在动作发生之前捕获它。

不知道这是否可能。PHP不会帮你做这些,因为它是在服务器端执行的,客户端在退出/重新加载时不会向服务器提交任何(有用的)东西,只有执行操作的命令。

这可能需要使用javascript侦听器,例如window。Onclose(虽然这显然不适用于safari或firefox 2),并在其中ajax xmlhttprequest发送数据到您的服务器。

对于失败的提交(即捕获表单中无效的数据),其几乎与提交工作的表单相同。当你处理数据的时候,再检查一下另一边的数据。