Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 表单的奇怪验证错误_Php_Forms_Validation - Fatal编程技术网

Php 表单的奇怪验证错误

Php 表单的奇怪验证错误,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

我得到的错误是:

注意:未定义的索引:在第22行的C:\xampp\htdocs\introductiongphp\includes\validation\u function.php中可见

这不应该发生,因为我已经实例化了所有变量,包括visible

验证函数.php

<?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";   
        }
    }
}