0

I am validating a form data using this script below. When i submit the form if there is any errors the error message is displaying properly but if no errors and validation succeed i try to echo out the variables to test the script but the script is only displaying this : [] Please examine the code and help me solve this.

    <?php

//included files
include("./includes/connect.php");
include("./includes/functions.php");
$errors = array();

//checking if user have submitted the form
if(isset($_POST['submitted'])) {
    //validating and cleaning submitted form data ... 
    if (isset($_POST['name']) && !empty($_POST['name'])) {
        if(preg_match("/^[a-zA-Z ]{2,20}$/", strip_trim($_POST['name']))) {
            $cln_name = clean_data($_POST['name']);
        } else {
            $_POST['name'] = FALSE;
            $errors[] = "The name you entered is not valid";
        }

    } else {
        $errors[] = "You have not enter your name!";
    }

    if(isset($_POST['email']) && !empty($_POST['email'])) {
        $cln_email = filter_var($_POST['email'] , FILTER_SANITIZE_EMAIL);
        if(filter_var($cln_email, FILTER_VALIDATE_EMAIL)) {
            $cln_email = clean_data($cln_email);
        } else {
            $_POST['email'] = FALSE;
            $errors[] = "The email you entered is not valid";
        }
    } else {
        $errors[] = "You have not provide you email!";
    }

    if(isset($_POST['plate_num']) && !empty($_POST['plate_num'])) {
        if(ctype_alnum($_POST['plate_num']) && strlen($_POST['plate_num']) >= 5) {
            $cln_plate_num = clean_data($_POST['plate_num']);
        } else {
            $_POST['plate_num'] = FALSE;
            $errors[] = "The plate number you provided is not a valid plate number";
        }
    } else {
        $errors[]= "You have not provide a plate number";
    }
    //checking for errors and printing errors.. 
    if (count($errors > 0)) {

        $errors_to_json = json_encode($errors);
        echo $errors_to_json;
        //foreach ($errors as $error) {
            //echo $error . "<br />";
        //}
    } else {
        echo $cln_name . "<br />";
        echo $cln_email . "<br />";
        echo $cln_plate_num;
    }
} else {
    echo "You did not submit the form!";
}

?>

This script is returning only this : []

Any idea please ??


functions.php :

 <?php

function clean_data($data) {
    if(function_exists('mysql_real_escape_string')) {
        global $dbc;
        $data = mysql_real_escape_string(trim($data), $dbc);
        $data = strip_tags($data);
    } else {
        $data = mysql_escape_string(trim($data));
        $data = strip_tags($data);
    }
    return $data;
}


function strip_trim($data) {
    $data = stripslashes(trim($data));
    return $data;
}



?>
5
  • which file did the function clean_data() is defined? can you show me the contents of it? I assume it is in./includes/functions.php right? Commented Jul 26, 2017 at 22:01
  • I updated the post Commented Jul 26, 2017 at 22:05
  • What is this? Why are you using this? global $dbc; Commented Jul 26, 2017 at 22:10
  • $dbc is my database connection Commented Jul 26, 2017 at 22:11
  • Are using MySQLI or MySQL? Review your code again. Commented Jul 26, 2017 at 22:13

1 Answer 1

1

you have problem in your if condition:

//checking for errors and printing errors.. 
if (count($errors > 0)) {
...

this will always return to TRUE because $error = [] and count([] > 0) results to TRUE

that's why you always end up in:

$errors_to_json = json_encode($errors);
echo $errors_to_json;
// Will indeed display '[]' because json_encode([]) is '[]'

i believe what you mean here is:

if (count($errors) > 0) {
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.