arrays - Notice: Undefined index - PHP Error -
i'm trying follow lynda's php tutorial , i'm stuck validations pages crud. i've been trying figure out went wrong can't seem find error coming though it's stated in error. i'm confused because i'm using same validation creating subject , executes fine creating page, i'm getting error - notice: undefined index: visible in d:\wd-stuff\ampps\www\cms.widgetcorp\includes\validation.php on line 14
.
below full code i'm working on.
ps: have radio input named visible.
ps2: i've read link tackled undefined index didn't answer question. same validation works fine form (new_subject.php) same new_page.php.
new_page.php
require_once '../includes/session.php'; require_once '../includes/functions.php'; require_once '../includes/connection.php'; require_once '../includes/validation.php'; getselectedcontent(); if (!$is_subject) { redirect_to("manage_content.php"); } if (isset($_post['submit'])) { // validations $req_fields = array("menu_name", "position", "visible", "content"); validatepresence($req_fields); $field_with_length = array ("menu_name" => 30); validatelenght($field_with_length); if (empty($errors)) { $s_id = $is_subject['id']; $menu_name = mysqlprep($_post['menu_name']); $position = (int) $_post['position']; $visible = (int) $_post['visible']; $content = mysqlprep($_post['content']); $qry = "insert pages ("; $qry .= " subject_id, menu_name, position, visible, content"; $qry .= ") values ("; $qry .= " {$s_id}, '{$menu_name}', {$position}, {$visible}, '{$content}'"; $qry .= ")"; $result = mysqli_query($conn, $qry); if ($result) { $_session['msg'] = "page added."; redirect_to("manage_content.php?subject=" . urlencode($is_subject['id'])); } else { $_session['msg'] = "page creation failed"; } } } include_once '../includes/layouts/header.php';
validation.php
$errors = array(); function isdatapresent($value) { return (isset($value) && $value !== ""); } function validatepresence($fields) { global $errors; foreach ($fields $field) { $value = trim($_post[$field]); if (!isdatapresent($value)) { $errors[$field] = str_replace("_", " ", ucfirst($field)) . " can't blank"; } } } function islengthvalid($value, $max) { return (strlen($value) <= $max); } function validatelenght($fields) { global $errors; foreach ($fields $field => $max) { $value = trim($_post[$field]); if (!islengthvalid($value, $max)) { $errors[$field] = str_replace("_", " ", ucfirst($field)) . " long"; } } }
i've thought of combining two. main goal of post understand why validation works 1 form not other same.
new_subject.php
require_once '../includes/session.php'; require_once '../includes/functions.php'; require_once '../includes/connection.php'; require_once '../includes/validation.php'; if (isset($_post['submit'])) { $menu_name = mysqlprep($_post['menu_name']); $position = (int) $_post['position']; $visible = (int) $_post['visible']; // validations $req_fields = array("menu_name", "position", "visible"); validatepresence($req_fields); $field_with_length = array ("menu_name" => 30); validatelenght($field_with_length); if (!empty($errors)) { $_session['errors'] = $errors; redirect_to('new_subject.php'); } $qry = "insert subjects ("; $qry .= "menu_name, position, visible"; $qry .= ") values ("; $qry .= " '{$menu_name}', $position, $visible"; $qry .= ")"; $result = mysqli_query($conn, $qry); if ($result === false) { $_session['msg'] = "subject creation failed"; redirect_to("new_subject.php"); } $_session['msg'] = "subject created"; redirect_to("manage_content.php"); } else { redirect_to("new_subject.php"); }
subject form
<form action="create_subject.php" method="post"> <p>menu name: <input type="text" name="menu_name" value=""> </p> <p>position: <select name="position"> <?php $subject_set = getallsubjects(); $subject_cnt = mysqli_num_rows($subject_set); ($cnt = 1; $cnt <= $subject_cnt + 1; $cnt++) { echo "<option value=\"{$cnt}\">{$cnt}</option>"; } ?> </select> </p> <p>visible: <input type="radio" name="visible" value="0"> no <input type="radio" name="visible" value="1"> yes </p> <input type="submit" name="submit" value="create subject"> </form>
page form
<form action="new_page.php?subject=<?= urlencode($is_subject['id']); ?>" method="post"> <p>menu name: <input type="text" name="menu_name" value=""> </p> <p>position: <select name="position"> <?php $page_set = getallpages($is_subject['id']); $page_cnt = mysqli_num_rows($page_set); ($cnt = 1; $cnt <= $page_cnt + 1; $cnt++) { echo "<option value=\"{$cnt}\">{$cnt}</option>"; } ?> </select> </p> <p>visible: <input type="radio" name="visible" value="0"> no <input type="radio" name="visible" value="1"> yes </p> <p>content: <br> <textarea rows="20" cols="80" name="content"></textarea> </p> <input type="submit" name="submit" value="create page"> </form>[validation error i'm trying reproduce][1]
that's because of bug in validatepresence() function, tries check value against condition, when value not set, hence why e_notice thrown. change code of function this:
function validatepresence($fields) { global $errors; foreach ($fields $field) { if (!isset($_post[$field]) || trim($_post[$field]) == '') { $errors[$field] = str_replace("_", " ", ucfirst($field)) . " can't blank"; } } }
then, can safely delete isdatapresent() function code, it's not needed anymore after change.
Comments
Post a Comment