dsfsdf5646 2016-06-22 09:34
浏览 46

$ _POST [“visible”]标记为未定义索引[重复]

I'm working on a PHP project, where I have the following form to submit:

<h2>Create Subject</h2>
   <form action="create_subject.php" method="post">
       <p>Subject name:
           <input type="text" name="menu_name" value="" />
       </p>
       <p>Position:
           <select name="position">
               <?php
               $subject_set = find_all_subjects();
               $subject_count = mysqli_num_rows($subject_set);
               for ($count=1; $count <= ($subject_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>
       <input type="submit" name="submit" value="Create Subject" />
    </form>`

In the create_subject.php (where the form action takes place), I have some validation, which looks like:

if(isset($_POST['submit'])) {
    // Process the form
    $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");
    }

where the validate presence should function as check if the fields are empty and looks like:

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

But when I submit the form with missing data, instead of redirecting back to the previous page and listing all the errors stored in a session, I get the following error messages:

  • Notice: Undefined index: visible in /Users/eak/Sites/widget_corp/public/create_subject.php on line 10

  • Notice: Undefined index: visible in /Users/eak/Sites/widget_corp/includes/validation_functions.php on
    line 22

  • Warning: Cannot modify header information - headers already sent by (output started at
    /Users/eak/Sites/widget_corp/public/create_subject.php:10) in
    /Users/eak/Sites/widget_corp/includes/functions.php on line 4

So the output started at where the $_POST["visible"] was detected as undefined. What can be the solution here?

</div>
  • 写回答

2条回答 默认 最新

  • duanmou9228 2016-06-22 09:40
    关注

    The solution is very simple. As you said $_POST["visible"] is undefined, simply use the following code block to handle the same.

    if (isset($_POST["visible"])) {
        //Handle the null condition
    }
    

    EDIT: As per your comment, you have a function to handle the said issue, but its called after you have used the value. Check below comments:

        $menu_name = mysql_prep($_POST["menu_name"]);
        $position = (int) $_POST["position"];
        $visible = (int) $_POST["visible"];  // YOU USED THIS VALUE - ERROR ALREADY OCCURRED HERE
    
        //validations
        $required_fields = array("menu_name", "position", "visible");  //AND THEN YOU PERFORMED VALIDATION-- CALL THIS BEFORE HAND
        validate_presences($required_fields);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大