【问题标题】:Preserve form values on validation在验证时保留表单值
【发布时间】:2012-12-26 15:39:37
【问题描述】:

我想防止在验证后重置字段。我已经搜索了一些主题,但我无法在我的代码中实现它。

if($form_view == true) { 
echo ''; 
if($error != '') { 
    echo '<strong><font color="#FF0000">Fout:</font></strong><br />'.$error.'<br />'; 
} 

echo '<form method="POST" action="'.$filename.'" style="border:0px; margin:0px; padding:0px;"> 
Voornaam
<br /> 
<input type="text" name="voornaam" maxlength="50" id="input_contact" style="width: 200px; value="'.(isset($_POST['voornaam']) ? $_POST['voornaam'] : '').'"> 
<br />
Achteraam 
<br /> 
<input type="text" name="achternaam" maxlength="50" id="input_contact" style="width: 200px; value="'.(isset($_POST['achternaam']) ? $_POST['achternaam'] : '').'"> 
<br />
Adres 
<br /> 
<input type="text" name="adres" maxlength="50" id="input_contact" style="width: 400px; value="'.(isset($_POST['adres']) ? $_POST['adres'] : '').'"> 
<br />
Postcode <h7><i><small>(1234 AB)</small></i></h7>
<br /> 
<input type="text" name="postcode" maxlength="7" id="input_contact" style="width: 100px; value="'.(isset($_POST['postcode']) ? $_POST['postcode'] : '').'"> 
<br />
Woonplaats 
<br /> 
<input type="text" name="woonplaats" maxlength="50" id="input_contact" style="width: 200px; value="'.(isset($_POST['woonplaats']) ? $_POST['woonplaats'] : '').'"> 
<br />
Telefoonnummer <h7><i><small>(0123-456789)</small></i></h7> 
<br /> 
<input type="text" name="telefoonnummer" maxlength="11" id="input_contact" style="width: 100px; value="'.(isset($_POST['telefoonnummer']) ? $_POST['telefoonnummer'] : '').'">
<br /><br /> 

如果你能给我一个例子,说明如何在一个领域实现这一点。

非常感谢

【问题讨论】:

  • 最后一个echoed 字符串是否在某处结束?
  • “字段重置”是什么意思?而且,您是使用 javascript 进行验证(也许是使用 jQuery 验证),还是使用 php 进行验证?还是两者兼而有之?

标签: php forms validation reset


【解决方案1】:

如果帖子不是针对他们登陆的特定页面,则 $_POST 变量将不包含该信息。

快速而肮脏的是将$_SESSION[]中提交的信息保存为数组。

当表单提交时,除了做你当前正在做的事情之外,你还需要将提交到会话中的信息保存在接收脚本上。

if ($_POST['submit']) {
// repeat or configure as desired to save submitted fields into Session
$_SESSION['form_info']['email_address'] = $_POST['email_address'];
}

在表单页面本身上,您将使用以下内容

if (isset($_SESSION['form_info'])) {
// You'll want to most likely consider filtering these using appropriate functions.
<input type="text" name="email_address" maxlength="50" id="email_address" style="width: 200px; value="<?php if (isset($_SESSION['form_info']['email_address']) { $_SESSION['form_info']['email_address']; }  ?>">

}

【讨论】:

    【解决方案2】:

    表单操作变量 $filename 是指这个特定的 php 文件还是其他发生验证的地方?如果表单操作发生在其他地方,那么 Moylin 的答案就是您的解决方案(使用 $_SESSION)。否则,如果可能,在此处进行验证是合理且简单的。正如你所做的那样,将 $_POST 回显到值字段应该足够了(如果你想要短代码,你甚至不需要那里的三元运算)。

    <form method="POST" action="thisPhpFile.php" >
    
    value ="'. @$_POST['voornaam'] .'"
    

    在验证部分(在回显表单之前),您自然要检查所有错误、必填字段等。如果一切都有效,只需将用户指向想要的位置。大致如下:

    if(isset($_POST['mySubmit'] {
        //check errors/validate
        if($valid == true) {
        Header("Location: yourLocation.php");
        }
    }
    else {
        //echo your form here
    }
    

    【讨论】:

    • 谢谢各位。我仍然不明白如何实现这一点。 @art2 是的,我将所有内容都放在一个 php 文件中。我使用了来自forum.fnl.nl/showthread.php?t=1836 的代码,也许这会有所帮助。非常感谢!
    猜你喜欢
    • 2016-07-20
    • 2013-11-03
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多