Skip to content
Advertisement

Php Filter array

I need a simple example of php filter array. This function is new for me so please I can’t understand complex program of it. I want to store filtered data to the database from a simple form where these conditions can exist. i.e: NAME in capital words. proper email/Password syntax. proper address and mobile number (1234-1234567) registration no: 2012-2015-AUP-1234 if any one mistakes an alert or message is displayed.

Advertisement

Answer

array_filter : The official documentation for PHP is full of very understandable examples for each function. These examples are given by the documentation or the community, and follows each function description. PHP Documentation

Form validation : A little code example for the script called by your POST form. Of course a lot of changes to this can be made to display the errors the way you like, and perhaps tune more precise checks on each data.

<?php
if (isset($_POST['name']) && !ctype_upper($_POST['name'])) {
    $errors[] = 'name should be uppercase';
}
if (isset($_POST['email']) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $errors[] = 'email is invalid';
}
if (isset($_POST['password']) && strlen($_POST['password']) >= 6 && strlen($_POST['password']) <= 16)) {
    $errors[] = 'password should be between 6 and 16 characters length';
}
if (isset($errors)) {
    // do not validate form
    echo '<ul><li>' . join('</li><li>', $errors) . '</li></ul>';
    // ... include the html code of your form here
}
else {
    // ... call things that must work on validated forms only here
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement