当出现错误时,如何将用户重新填写的表单字段重新填写到表单中? [英] How can I re-fill form fields that a user filled out after I've redirected them back to the form when there was an error?
问题描述
简短版本:
- 我有一个包含100多个字段的表单,每个字段都有服务器端验证.
- 我的表格正在工作并且数据已成功提交;验证发生错误时,我什至将它们重定向回表单,并显示具体发生了什么错误.
- 将他们重定向回(大量)表格后,所有数据都消失了,需要再次输入.
我已经想到了几种回答我自己的问题的方法,但是它们并不是目前为止最优雅的解决方案.
I have already thought of a couple ways of answering my own question, but they aren't the most elegant solutions out there.
我当前的思维过程是:将所有输入的数据保存为$ _SESSION变量-每当他们加载表单时,就这样对每个输入执行 if
语句;
My current thought process is: save all of their inputted data as $_SESSION variables -- and whenever they load the form, do an if
statement around each input, as such;
if(isset($_SESSION['foo_data'])) {
echo "<input type='text' name='foo' value='$_SESSION["foo_data"]'>";
} else {
echo "<input type='text' name='foo' placeholder='Enter your text here...'>";
}
此方法将有效,但是它还涉及到对140多个会话变量的繁琐定义,然后围绕 EVERY 的if/else语句进行了更为繁琐和巨大的创建>表单字段.
This method will work, but it also involves an immensely tedious definition of 140+ session variables, followed by an even more tedious and immense creating if/else statements around EVERY form field.
还有更好的方法吗?
其他信息:使用纯PHP,没有任何框架.
Additional info: using plain php, no frameworks of any kind.
推荐答案
不需要会话.您的基本计划应该是:
Sessions are unnecessary. Your basic plan should be:
- 从$ _POST()获取变量
- 验证表单值.
- 使用通过验证的值创建一个数组.
- 重定向到表单时,使用数组填充值.
在我开始使用CodeIgniter之前,我曾经使用 if
语句来验证并生成如下错误消息:
Before I started using CodeIgniter, I used to use if
statements to validate and generate error messages like this:
if ( ! empty($_POST['email'])){
if ($this->verifyInput($_POST['email'], 6)){
$fill['email'] = $_POST['email'];//$fill is the array of values that passed validation
} else $tally++;//$tally keeps a running count of the total number of erors
}
我对 $ this-> verifyInput
进行函数调用,该函数接受字段值和要执行的验证类型-在这种情况下为6,表示电子邮件.这是我的验证功能:
I make a function call to $this->verifyInput
which accepts the field value and the type of validation to perform--in this case it's 6, which indicates email. Here's my validation function:
function verifyInput($input, $type){
if ($type == 0) $pattern = '/^1$/';//just the number 1
elseif ($type == 1) $pattern = '/^[0-9]+$/';//just integers
elseif ($type == 2) $pattern = '/^[A-Za-zÁ-Úá-úàÀÜü]+$/';//just letters
elseif ($type == 3) $pattern = '/^[0-9A-Za-zÁ-Úá-úàÀÜü]+$/';//integers & letters
elseif ($type == 4) $pattern = '/^[A-Za-zÁ-Úá-ú0-9àÀÜü\s()\/\'":\*,.;\-!?&#$@]{1,1500}$/';//text
elseif ($type == 5) $pattern = '/^[A-Za-zÁ-Úá-úàÀÜü0-9\']+[A-Za-zÁ-Úá-úàÀÜü0-9 \'\-\.]+$/';//name
elseif ($type == 6) $pattern = '/^.+@[^\.].*\.[a-z]{2,}$/';//e-mail
elseif ($type == 7) $pattern = '/^((\(0?[1-9][0-9]\))|(0?[1-9][0-9]))?[ -.]?([1-9][0-9]{3})[ -.]?([0-9]{4})$/';//brazilian phone
if (preg_match($pattern, $input) == 1) return true;
else return false;
}
验证完成后,我将使用 $ fill
数组为每个字段预填表单:
Once the validation is done, I use the $fill
array to prefill the form for each field:
<input class="field" type="text" name="email" value = "<?php echo $fill['email']; ?>"/>
您还应该确保 $ fill
包含空值,例如 $ fill ['email'] ='';
或检查以确保这些值在填充输入标签之前进行设置:
You should also make sure that $fill
is iniciated with empty values such as $fill['email']='';
or check to make sure the values are set before filling the input tags:
<input class="field" type="text" name="email" value = "<?php if(isset($fill['email'])) echo $fill['email']; ?>"/>
这些只是可以满足您的需求的一般准则,但是我发现这是处理它的便捷方法.
These are just general guidelines that you can adapt to your needs, but I found this to be a convenient way to handle it.
这篇关于当出现错误时,如何将用户重新填写的表单字段重新填写到表单中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!