/    Sign up×
Community /Pin to ProfileBookmark

How To Validate Inputs In Html 5 Form With php 7 ?

Php Experts,

Do you mind showing me how to validate the following user inputs with php ?

[code]
<form method = ‘POST’ action = “”>
<label for=’find’>Find</label>
<input type=’text’ name=’find’ id=’find’>
<br>
Table:
<input type=’radio’ name=’table’ id=’sale’><label for=’table’>Businesses On Sale</label>
<input type=’radio’ name=’table’ id=’sold’><label for=’table’>Businesses Sold</label>
<br>
<label for=’business_name’>Business Name</label>
<input type=’text’ name=’business_name’ id=’business_name’>
<label for=’business_zip’>Business Zip</label>
<input type=’text’ name=’business_zip’ id=’business_zip’>
<label for=’business_phone’>Business Phone</label>
<input type=’text’ name=’business_phone’ id=’business_phone’>
<label for=’business_mobile’>Business Mobile</label>
<input type=’text’ name=’business_mobile’ id=’business_mobile’>
<label for=’business_fax’>Business Fax</label>
<input type=’text’ name=’business_fax’ id=’business_fax’>
<label for=’business_email’>Business Email</label>
<input type=’text’ name=’business_email’ id=’business_email’>
<label for=’business_description’>Business Description</label>
<input type=’text’ name=’business_description’ id=’business_description’>
<button type=’submit’>Submit!</button>
</form>
</body>
</html>
[/code]

I tried my best here:
https://www.webdeveloper.com/d/395303-what-is-wrong-with-my-submit-form
And messed things up on my php. And so, starting all over from scratch over here now. But with your foundation this time.
I’d appreciate code samples from your ends, ofcourse. Samples that don’t use regex unless there’s no other way. Still at procedural style, mysqli and prepared statements. No pdo or oop yet. So bear this in mind when providing sample codes (if any) as I got to understand your codes. Not interested just copy & pasting lines I don’t understand. Need to learn. Proper way. Your way.

Thank you.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 14.2021 — 
  • 1. Create a function for each type of validation you decide you need to do.

  • 2. Create an array that has a key for each form field name that needs to be validated, and has a sub-array with the name of the function you would use to validate it, and a second field for whether or not it is required. Then you can do something like:
    [code=php]
    $fields = [
    'first_name' => [
    'required' => true,
    'validator' => 'name'
    ],
    'last_name' => [
    'required' => true,
    'validator' => 'name'
    ],
    'age' => [
    'required' => false,
    'validator' => 'valid_integer'
    ]
    ];

    $errors = [];
    foreach($fields as $key => $field_info) {
    if($field_info['required'] and(!isset($_POST[$key]) or trim($_POST[$key] === ''))) {
    $errors[$key] = 'Required';
    } elseif(isset($_POST[$key])) {
    if($field_info['validator']($_POST[$key]) == false) {
    $errors[$key] = "Invalid";
    }
    }
    }
    if(!empty($errors)) {
    // use keys/values in $errors to output errors, and whatever else you
    // want to do if an invalid form submission
    } else {
    // go ahead and process the form
    }

    // these could be in a separate include file to be required in this script
    function name($value) {
    // 2 - 40 non-white-space characters
    return preg_match('/^S{2,40}$/', trim($value));
    }

    function valid_integer($value) {
    // integer > 0
    return ((string) intval($value) === trim($value) and intval($value) > 0);
    }
    [/code]
  • Copy linkTweet thisAlerts:
    @developer_webauthorJul 15.2021 — @NogDog#1634148

    Thank you very much! I think you put me in the right direction now.

    However, there's always a little hiccup or hesitation to start your first day in something, like school or job.

    And so, I got 2 hiccups. care to explain them ?

  • 1. This "validator" is new to me. Any link I can look up about it ? Or, you just created your custom thingy here ?

    2.

  • 2. Care to explain the regex ? Don't understand the **/** and **** **^** symbols. What they mean. Forgotten.

    function name($value) {

    // 2 - 40 non-white-space characters

    return preg_match('/^S{2,40}$/', trim($value));

    }


  • 3. Care to explain the following, especially the **bold** part here too ?

    function valid_integer($value) {

    // integer > 0

    return (**(string)** intval($value) === trim($value) and intval($value) > 0);

    }


  • Are you returning the INT as a STRING here ? If so, why ? Forcing the returned data to DATE TYPE: STRING.

    Yes ?

    (Remember, all this shorthand php is new to me).

    How would you write the following in beginner level way ? Long way that we beginners are used to ? I should be able to understand the long hand writing.

    **return ((string) intval($value) === trim($value) and intval($value) > 0);**

    Cheers!
    Copy linkTweet thisAlerts:
    @developer_webauthorJul 23.2021 — @NogDog#1634148

    Thank you very much! I think you put me in the right direction now.

    However, there's always a little hiccup or hesitation to start your first day in something, like school or job.

    And so, I got 2 hiccups. care to explain them ?

  • 1. This "validator" is new to me. Any link I can look up about it ? Or, you just created your custom thingy here ?

    2.

  • 2. Care to explain the regex ? Don't understand the **/** and **** **^** symbols. What they mean. Forgotten.

    function name($value) {

    // 2 - 40 non-white-space characters

    return preg_match('/^S{2,40}$/', trim($value));

    }


  • 3. Care to explain the following, especially the **bold** part here too ?

    function valid_integer($value) {

    // integer > 0

    return (**(string)** intval($value) === trim($value) and intval($value) > 0);

    }


  • Are you returning the INT as a STRING here ? If so, why ? Forcing the returned data to DATE TYPE: STRING.

    Yes ?

    (Remember, all this shorthand php is new to me).

    How would you write the following in beginner level way ? Long way that we beginners are used to ? I should be able to understand the long hand writing.

    **return ((string) intval($value) === trim($value) and intval($value) > 0);**

    Cheers!
    Copy linkTweet thisAlerts:
    @developer_webauthorSep 21.2021 — @NogDog,

    Care to address my previous post ?

    Thank You! :)
    ×

    Success!

    Help @developer_web spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.25,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...