php FORM VALIDATION NOT WORKING..!!!

I’ve been trying to Validate the following form with the following functions :

[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_functions.php';?> <?php if (isset($_POST['submit'])) { $menu_name = mysql_prep($_POST["menu_name"]); $position = (int) $_POST["position"]; $visible = (int) $_POST["visible"]; //validations $required_fields = array("menu_name", "position", "visible"); validate_presence($required_fields); $fields_with_max_lengths = array("menu_name" => 30); validate_max_lenghts($fields_with_max_lengths); if (!empty($errors)) { $_SESSION["errors"] = $errors; redirect_to("new_subject.php"); } $query = "INSERT INTO subjects ("; $query .= " menu_name, position, visible "; $query .= " ) VALUES ( '{$menu_name}', {$position}, {$visible} "; $query .= ")"; $result = mysqli_query($connection, $query); if ($result) { // Success $_SESSION["message"] = "Subject Created."; redirect_to("manage_content.php"); } else { //Failure $_SESSION["message"] = "Subject Creation Failed."; redirect_to("new_subject.php"); } } else { // This is probably a GET request redirect_to("new_subject.php"); } ?> <?php if (isset($connection)) { mysqli_close($connection); } ?>[/php]

The page above is the page in which proccesing is done the problem with it is that it accepts any data I want it to check whether the form is empty or not or is it long or not but It creates the subject without checking it.
This is the page where the functions for the validation is defined:
[php]<?php
$errors = array();

function fieldname_as_text($fieldname) {
    $fieldname = str_replace("_", " ", $fieldname);
    $fieldname = ucfirst($fieldname);
    return $fieldname;
}

// prescence 
// use trim() so empty spaces don't count
// use === to avoid false positivies
// empty() would consider "0" to be empty
function has_presence($value) {
    return isset($value) && $value !== "";
}

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

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

function validate_max_lenghts($fields_with_max_lenghts) {
    global $errors;
    // expects an assoc. array
    foreach ($fields_with_max_lenghts as $field => $max) {
        $value = trim($_POST[$field]);
        if (!has_max_length($value, $max)) {
            $errors[$field] = fieldname_as_text($field) . " is too long.";
        }
    }
}

function has_inclusion_in($value, $set) {
    return in_array($value, $set);
}

?>
and this is the page that the form exist:

<?php require_once '../includes/session.php';?> <?php require_once '../includes/db_connection.php';?> <?php require_once '../includes/functions.php';?> <?php include '../includes/layouts/header.php'; ?> <?php find_selected_page(); ?>
       <div id="main">
            <div id="navigation">
               <?php echo navigation($current_subject, $current_page); ?>
            </div>
            <div id="page">
                <?php echo message(); ?>
                <h2>Create Subject</h2>
                <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 = 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>
                <br />
                <a href="manage_content.php">Cancel</a>
            </div>
        </div>
<?php include '../includes/layouts/footer.php'; ?>

[/php]

[php] function validate_max_lenghts($fields_with_max_lenghts) {
global $errors;
[/php]

In this line, you re-assign $errors, so that any error you recorded with validate_presence is lost.

Sponsor our Newsletter | Privacy Policy | Terms of Service