[英]PHP Not Reading Form Fields As Empty
everyone! 大家! I've been having a terrible time trying to figure out why my form validation doesn't recognize empty fields and return them as errors.
我一直在费劲地试图弄清楚为什么我的表单验证无法识别空字段并将其返回为错误。
Here is a function I use to check the fields: 这是我用来检查字段的函数:
function check_required_fields($required_array) {
$field_errors = array();
foreach($required_array as $fieldname) {
if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) {
$field_errors[] = $fieldname;
}
}
return $field_errors;
}
Then, my form is process like this: 然后,我的表单就是这样的过程:
if (isset($_POST['submit'])) { // Form has been submitted.
$errors = array();
// perform validations on the form data
$required_fields = array('client', 'banner', 'district', 'store', 'position', 'first_name', 'last_name', 'email', 'password');
$errors = array_merge($errors, check_required_fields($required_fields, $_POST));
and an IF statement checks for errors: IF语句检查错误:
if ( empty($errors) ) {
$query =
The problem is, even if my fields are empty (and I checked them with var_dump($_POST)) PHP performs the $query. 问题是,即使我的字段为空(并且我用var_dump($ _ POST)检查了它们),PHP也会执行$ query。 I think there is something wrong with the function.
我认为该功能有问题。 I hope someone can help me spot the error!
希望有人可以帮助我发现错误!
Here is the gist of my HTML code: 这是我的HTML代码的要点:
<form action="registration.php" method="post">
<label for="first_name" class="medium">First Name</label>
<input type="text" name="first_name" class="textbox short_field">
<button type="submit" name="submit" value="submit" class="form-submit-button">SUBMIT</button>
</form>
I also use the following functions to validate string length and it works just fine: 我还使用以下函数来验证字符串长度,并且可以正常工作:
function check_max_field_lengths($field_length_array) {
$field_errors = array();
foreach($field_length_array as $fieldname => $maxlength ) {
if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $field_errors[] = $fieldname;
}
}
return $field_errors;
}
and 和
$fields_with_lengths = array('first_name' => 30, 'last_name' => 30, 'email' => 50, 'password' => 30);
$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));
"The problem is, even if my fields are empty (and I checked them with var_dump($_POST)) PHP performs the $query."
“问题是,即使我的字段为空(并且我用var_dump($ _ POST)检查了它们),PHP也会执行$ query。”
Your problem is caused by this condition: $var != 0. Php can cast variables of different types between each other, so empty string is equal to 0. Proof: 您的问题是由以下情况引起的:$ var!=0。Php可以在彼此之间强制转换不同类型的变量,因此空字符串等于0。
<?php
$var = '';
var_dump($var == 0);
Output: bool(true)
输出:
bool(true)
Now look at this sample, imitating, how php processes your condition: 现在来看这个示例,模仿一下php如何处理您的条件:
<?php
$var = '';
var_dump(empty($var));
var_dump((empty($var) && $var != 0));
Output: 输出:
bool(true)
bool(false)
In the 2-nd case the boolean function returns false, therefore, the variable won't be added to the array of errors. 在第二种情况下,布尔函数返回false,因此,该变量不会添加到错误数组中。
I don't know, what do you want to achieve with this: $var != 0
. 我不知道,您要用什么实现:
$var != 0
。 But the condition: 但条件:
if (!isset($_POST[$fieldname]) ||
(empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) {
$field_errors[] = $fieldname;
}
can be easily switched to this: 可以很容易地切换到这个:
if (empty($_POST[$fieldname])) {
$field_errors[] = $fieldname;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.