Php 表单的奇怪验证错误
我得到的错误是: 注意:未定义的索引:在第22行的C:\xampp\htdocs\introductiongphp\includes\validation\u function.php中可见 这不应该发生,因为我已经实例化了所有变量,包括visible 验证函数.phpPhp 表单的奇怪验证错误,php,forms,validation,Php,Forms,Validation,我得到的错误是: 注意:未定义的索引:在第22行的C:\xampp\htdocs\introductiongphp\includes\validation\u function.php中可见 这不应该发生,因为我已经实例化了所有变量,包括visible 验证函数.php <?php $errors = array(); function fieldname_as_text($fieldname) { $fieldname = str_replace("_", " ", $fie
<?php
$errors = array();
function fieldname_as_text($fieldname) {
$fieldname = str_replace("_", " ", $fieldname);
$fieldname = ucfirst($fieldname);
return $fieldname;
}
// * presence
// use trim() so empty spaces don't count
// use === to avoid false positives
// empty() would consider "0" to be empty
function has_presence($value) {
return isset($value) && $value !== "";
}
function validate_presences($required_fields) {
global $errors;
foreach($required_fields as $field) {
$value = trim($_POST[$field]);
if (!has_presence($value)) {
$errors[$field] = fieldname_as_text($field) . " can't be blank";
}
}
}
// * string length
// max length
function has_max_length($value, $max) {
return strlen($value) <= $max;
}
function validate_max_lengths($fields_with_max_lengths) {
global $errors;
// Expects an assoc. array
foreach($fields_with_max_lengths as $field => $max) {
$value = trim($_POST[$field]);
if (!has_max_length($value, $max)) {
$errors[$field] = fieldname_as_text($field) . " is too long";
}
}
}
// * inclusion in a set
function has_inclusion_in($value, $set) {
return in_array($value, $set);
}
?>
您可能在输入HTML字段上有输入错误。您可以使用:
if (isset($_POST[$field])) {
在validate_presences()
函数上,确保该值存在。当您尝试执行trim($\u POST[$field])
假设该字段存在于$\u POST
数组中-对于可见的,在这种情况下它不存在。您可以将trim
移动到has\u presence()
现在,如果变量存在,您将只使用修剪。好的,标记单选复选按钮使其现在工作。谢谢你们的投入,伙计们。它帮了我很大的忙。我按照先前的建议在has_presence进行了修剪,结果它没有认识到所有的价值观。我的visible实际上是一个tinyint,其值为0表示否,1表示是。这就是影响它的原因吗?回显验证存在()函数中的$field
和$\u POST[$field]
变量。它会给您一个提示。菜单\名称注意:未定义索引:在C:\xampp\htdocs\introductiongphp\includes\validation\u function.php第22行visiblecontent中可见,它似乎正在打印可见内容。虽然它们有点粘在一起。而且,你有没有标记其中一个单选按钮?您还可以尝试在错误行之前执行var\u dump($\u POST)
,以检出数组。这当然可以帮助您查看您从表单中收到了什么。是的,在标记一个单选按钮后,它现在可以工作了。非常感谢你
if (isset($_POST[$field])) {
function has_presence($value) {
return isset($value) && trim($value) !== "";
}
function validate_presences($required_fields) {
global $errors;
foreach($required_fields as $field) {
if (!has_presence($value)) {
$errors[$field] = fieldname_as_text($field) . " can't be blank";
}
}
}