I'm trying to follow Lynda's PHP tutorial and I'm stuck with the validations for the pages CRUD. I've been trying to figure out what went wrong but I can't seem to find where the error is coming from even though it's already stated in error. I'm confused because I'm using the same validation for creating subject and it executes just fine but with for creating page, I'm getting the error - Notice: Undefined index: visible in D:\wd-stuff\ampps\www\cms.widgetcorp\includes\validation.php on line 14
.
Below is the full code that I'm working on.
PS: I have a radio input named visible.
PS2: I've read the link that tackled the Undefined index but it didn't answer my question. The same validation works fine from another form (new_subject.php) which is basically the same as the 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 INTO 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 as $field) {
$value = trim($_POST[$field]);
if (!isDataPresent($value)) {
$errors[$field] = str_replace("_", " ", ucfirst($field)) . " can't be blank";
}
}
}
function isLengthValid($value, $max)
{
return (strlen($value) <= $max);
}
function validateLenght($fields)
{
global $errors;
foreach ($fields as $field => $max) {
$value = trim($_POST[$field]);
if (!isLengthValid($value, $max)) {
$errors[$field] = str_replace("_", " ", ucfirst($field)) . " is too long";
}
}
}
I've also thought of just combining the two. But the main goal of my post is to understand why the validation works to one form but not with the other which is basically the 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 INTO 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);
for ($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);
for ($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 that I'm trying to reproduce][1]