【问题标题】:Strange validation error for form表单的奇怪验证错误
【发布时间】:2014-01-18 11:44:39
【问题描述】:

我得到的错误是:

注意:未定义索引:在第 22 行的 C:\xampp\htdocs\introducingphp\includes\validation_function.php 中可见

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

Validation_function.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);
}

?>

new_page.php(具有进行验证的一页提交表单的页面)

<?php require_once("includes/session.php"); ?>
<?php require_once("includes/db_connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/validation_function.php"); ?>

<?php find_selected_page(); ?>

<?php
// Can't add a new page unless there is a subject as a parent
if (!$current_subject) {
    // subject ID was missing or invalid or
    //subject couldn't be found in database
    redirect_to("manage_content.php");
}
?>

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

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

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

    if (empty($errors)) {
      // perform Create

    //add the subject_id
    $subject_id = $current_subject["id"];
    $menu_name = mysql_prep($_POST["menu_name"]);
    $position = (int) $_POST["position"];
    $visible = (int) $_POST["visible"];
    //escape content
    $content = mysql_prep($_POST["content"]);

    // 2. Perform database query
    $query .= "INSERT INTO pages (";
    $query .= " subject_id, menu_name, position, visible,
    content";
    $query .= ") VALUES (";
    $query .= " {$subject_id}, '{$menu_name}', {$position},
    {$visible}, '{$content}'";
    $query .= ")";

    $result = mysqli_query($connection, $query);


    if ($result ) {
        // Success
        $_SESSION["message"] = "Page Created.";

        redirect_to("manage_content.php?subject=" .
        urlencode($current_subject["id"]));
    }else {
        // Failure
        $_SESSION["message"] = "Page creation failed.";
    }   
  }
} else {
     // This is probably a GET request

} // End: If(isset($_POST['submit']))

?>
<?php $layout_context = "admin"; ?>
<?php include("header.php"); ?>
<div id="main">
<div id="navigation">
<?php echo navigation($current_subject, $current_page); ?>
</div>
<div id="page">
<?php echo message(); ?>
<?php echo form_errors($errors); ?>

<h2>Create Page</h2>
<form action="new_page.php?subject=<?php echo
urlencode($current_subject["id"]); ?>" method="post">
<p>Menu name:
<input type="text" name="menu_name" value="" />
</p>
<p>Position:
<select name="position">
<?php
$page_set =
find_all_pages_for_subject($current_subject["id"], false);
$page_count = mysqli_num_rows($page_set);
for($count=1; $count <= ($page_count + 1); $count++) {
   echo "<option value=\"{$count}\">{$count}</option>";   
}
?>
</select>
</p>
<p>Visible
<input type="radio" name="visible" value="0" /> NO
&nbsp;
<input type="radio" name="visible" value="1" /> Yes
</p>
<p>Content:<br />
<textarea name="content" rows="20" cols="80"></textarea>
</p>
<input type="submit" name="submit" value="Create Page" />
</form>
<br />
<a href="manage_content.php?subject=<?php echo
urlencode($current_subject["id"]); ?>">Cancel</a>
</div>
</div>

<?php include("includes/footer.php"); ?>

【问题讨论】:

    标签: php forms validation


    【解决方案1】:

    您可能在输入 HTML 字段中有错字。您可以使用:

    if (isset($_POST[$field])) {
    

    validate_presences() 函数上确保该值存在。

    【讨论】:

    • 我遵循了之前在 has_presence 处进行修剪的建议,但最终无法识别所有值。我的可见实际上是一个 tinyint,其值为 0 表示否,1 表示是。这就是影响它的原因吗?
    • validate_presences() 函数中回显$field$_POST[$field] 变量。它会给你一个提示。
    • menu_name 注意:未定义的索引:在 C:\xampp\htdocs\introducingphp\includes\validation_function.php 中可见的第 22 行可见内容似乎正在打印可见和内容。虽然他们有点粘在一起。
    • 还有,您是否标记了其中一个单选按钮?您也可以尝试在错误行之前执行var_dump($_POST) 以检查数组。这肯定可以帮助您查看从表单中收到的内容。
    • 是的,在标记其中一个单选按钮后,它现在可以工作了。非常感谢
    【解决方案2】:

    当您尝试执行trim($_POST[$field]); 时,您假设该字段存在于$_POST 数组中 - 对于visible,在这种情况下不存在。您可以将trim 移动到has_presence()

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

    现在,如果变量存在,您将只有 trim

    【讨论】:

      【解决方案3】:

      好的,标记单选检查按钮使其现在可以工作。感谢您的所有投入。这对我帮助很大。

      【讨论】:

        猜你喜欢
        • 2019-12-01
        • 1970-01-01
        • 2021-04-21
        • 1970-01-01
        • 2017-10-22
        • 1970-01-01
        • 1970-01-01
        • 2021-01-23
        • 1970-01-01
        相关资源
        最近更新 更多