Best way of validating html select options.
Results 1 to 5 of 5

Thread: Best way of validating html select options.

  1. #1
    Member
    Join Date
    Jan 2017
    Location
    Michigan / United States
    Posts
    46

    Best way of validating html select options.

    Can someone tell me which of these two methods would be best.
    Or perhaps suggest another method.

    PHP Code:
    if (isset($_POST['Submit'])) { 
            if(isset(
    $_POST['req-state'])) { 
                if(
    $_POST['req-state'] ! == 'NULL') && ($_POST['req-state'] ! == ""){ 
                    
    $_POST['req-state'] = filter_var($_POST['req-state'], FILTER_SANITIZE_STRING);
                if (
    $_POST['req-state'] == "") {
                    
    $errors .= 'Please enter a valid state.<br/><br/>'
                } 
                else { 
                    echo 
    '<p>You have selected: <strong>'$_POST['state'], '</strong>.</p>'
                } 
            } 
        } 
    PHP Code:
    if(isset($_POST['req-state']) {
            if(
    $_POST['req-state'] ! == 'NULL') && ($_POST['req-state'] ! == "") {
                
    $_POST['req-state'] = $newState;
            } 
            if(!
    in_array($newState, array("Alabama""Alaska""Arizona" "rest of the states"))) {
                
    $errors .= 'Please enter a valid state.<br/><br/>';
            }
            else { 
                    echo 
    '<p>You have selected: <strong>'$newState'</strong>.</p>'
                } 
        } 

  2. #2
    Senior Member
    Join Date
    Apr 2016
    Posts
    121
    You would use an array, holding all the possible choices, in order to make sure that the submitted value is a permitted choice. However, you wouldn't define the array in the validation logic. You would define/retrieve it at a common point in the form and the form processing code (such as in a configuration file or a database table) and you would also use it to dynamically build the list of <option ></option> choices for the form, so that both the form and the form processing code use the exact same array of data.

    After the form has been submitted, a <select> field will be set, so, don't use isset() in your code. If a <select> field isn't set, it either means that you have a mistake in your coding somewhere or someone is submitting their own data to your form processing code. In either case, you would want to see/log a php error about a non-existent field in the submitted form data.

    The value will never be the literal string 'NULL' (what your code is testing for now), nor will it ever normally be a php NULL value, so, there's no good reason to test against a null value. If the 1st/default option choice is actually a prompt to make a selection and its value in an empty string - '', you would test if the submitted value is an empty string when the form field is 'required' and setup an appropriate error message telling the visitor that they must make a selection.

    If the submitted value isn't an empty string, you would test if it is in the array of possible choices, using in_array(), and setup an appropriate message that the value isn't a valid choice. You would only see this error message when testing and only if you have a mistake in your form or in your form processing code. An actual visitor to your site will never see this error.

    I also recommend that you use an array to hold the valuation error messages, with the array element index name being the form field name. This will let you test at any point if there is a validation error for any field (some fields might require multiple validation steps and you would only run successive steps if there are no errors for that field from an earlier step) and will let you output any error messages next to the form field they correspond to, by accessing errors by their field name.
    Last edited by pbismad; 08-17-2017 at 09:01 AM.
    Programming should not be a painful activity. If you are experiencing pain while programming, you are probably doing something wrong.

  3. #3
    Member
    Join Date
    Jan 2017
    Location
    Michigan / United States
    Posts
    46
    @pbismad Thank you for your reply.
    The value will never be the literal string 'NULL' (what your code is testing for now), nor will it ever normally be a php NULL value, so, there's no good reason to test against a null value. If the 1st/default option choice is actually a prompt to make a selection and its value in an empty string - '', you would test if the submitted value is an empty string when the form field is 'required'
    This would not be the literal string "NULL" ?
    HTML Code:
    <select name="req-state" class="required" value="" /><option value="NULL">Please select your state</option>

  4. #4
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,843
    Quote Originally Posted by New_PHP_Guy View Post
    @pbismad Thank you for your reply.

    This would not be the literal string "NULL" ?
    HTML Code:
    <select name="req-state" class="required" value="" /><option value="NULL">Please select your state</option>
    If that is how you define your HTML form, then yes, the actual value would be the string 'NULL' if selected (not the PHP primitive type NULL). It's a bit confusing since we often use NULL in SQL and PHP code not as a string, but in this case, yes, it's just a string.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Senior Member
    Join Date
    Apr 2016
    Posts
    121
    This would not be the literal string "NULL" ?
    It would, but using a value that looks like, but isn't actually, a common keyword in programming languages isn't wise.
    Programming should not be a painful activity. If you are experiencing pain while programming, you are probably doing something wrong.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •