PHP表单验证功能不起作用

时间:2015-06-15 08:15:42

标签: php

我正在关注迄今为止一直很棒的视频教程系列。我被困在一个给你一些表格验证功能的部分。实际表单目前位于此处:http://leegster.com/leegster_test/public/manage_content.php

以下是在create_subject.php中调用函数的方法:

if (isset($_POST['submit'])) {
  // Process the form

  //$menu_name = $_POST["menu_name"];
  $menu_name = mysql_prep($_POST["menu_name"]);
  $position = (int) $_POST["position"];
  $visible = (int) $_POST["visible"];

  // validations
  $required_fields = array("menu_name", "position", "visible");
  validate_presences($required_fields);

  $fields_with_max_lengths = array("menu_name" => 30);
  validate_max_lengths($fields_with_max_lengths);

  if (!empty($errors)) {
      $_SESSION["errors"] = $errors;
      redirect_to("new_subject.php");
  }

以下是validation_functions.php中显示的函数:

// * 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";
        }
    }
}

提交表单时,会调用validate_presences和validate_max_lengths,以确保没有任何内容留空,且menu_name不超过30个字符。页面应该刷新并且应该显示错误(如果需要,我也可以包含此代码)。

但是,当我在表单上创建一个空白条目时,它允许我这样做。使用成功消息将空白条目提交给数据库。我无法理解为什么会发生这种新的验证函数,这些函数应该阻止它。据我所见,我通过视频教程一步一步地完成了所有工作。

非常感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

验证所需输入表单的简单方法是使用html5必需属性,并限制char可以使用maxlength属性 例如:

&#13;
&#13;
<input type="text" name="cat" maxlength="30" required>
&#13;
&#13;
&#13;

如果您仍想继续使用php,则必须包含脚本的完整内容。 纠正我,如果我错了,因为我也是新手:)

答案 1 :(得分:0)

刚刚测试了您的代码,它现在似乎应该可以运行了:

function has_presence($value) {
    return isset($value) && $value != ""; // you forgot $, propably a typo
}

function validate_presences($required_fields, $fields) {
    $errors = []; // do not use globals, you should pass all required variables as arguments

    foreach($required_fields as $field) {
        $value = trim($fields[$field]);
        if (!has_presence($value)) {
            $errors[$field] = $field." can't be blank";
        }
    }

    return $errors;
}

function has_max_length($value, $max) {
    return strlen($value) <= $max;
}

function validate_max_lengths($fields_with_max_lengths, $fields) {
    $errors = [];

    // Expects an assoc. array
    foreach($fields_with_max_lengths as $field => $max) {
        $value = trim($fields[$field]);
        if (!has_max_length($value, $max)) {
            $errors[$field] = $field." is too long";
        }
    }

    return $errors;
}

$required_fields = array("menu_name", "position", "visible");
$errors = validate_presences($required_fields, $post);

$fields_with_max_lengths = array("menu_name" => 30);
$errors = array_merge($errors, validate_max_lengths($fields_with_max_lengths, $post));

print_r($errors);

但是,您应该始终检查字段是否存在。 此$value = trim($fields[$field]);应更改为: $value = isset($fields[$field]) ? trim($fields[$field]) : '';

哦,我也不喜欢这样: mysql_prep($_POST["menu_name"]); 你绝对应该首先逃离帖子字段。

相关问题