Home > Enterprise >  Validate checkboxes values in PHP
Validate checkboxes values in PHP

Time:10-28

I have a group of checkboxes with different values each. I want to assign their values in php variables which i'm going to send to database. The main problem is that i don't know how to check inside the php code if the values of selected items matching their default values which i setup in the html (apple == apple, samsung == samsung) and so on. This is because someone can just change the input value inside the console and insert whatever he likes in my DB. Any ideas how i can sort this out. Many thanks!

    <form action="" method="POST">
        <label for="apple">Apple</label>
        <input id="apple" type="checkbox" name="myCheckBoxes[]" value="Apple">
        <label for="samsung">Samsung</label>
        <input id="samsung" type="checkbox" name="myCheckBoxes[]" value="Samsung">
        <label for="lenovo">Lenovo</label>
        <input id="lenovo" type="checkbox" name="myCheckBoxes[]" value="Lenovo">
        <label for="google">Google Pixel</label>
        <input id="google" type="checkbox" name="myCheckBoxes[]" value="Google Pixel">

        <button type="submit" name="submit">Send</button>
    </form>

PHP Code:

if (isset($_POST['submit'])) {
    $checkBoxes = $_POST['myCheckBoxes'];
    $numberSelected = count($checkBoxes);
    
    if ($numberSelected > 3) {
        echo 'Please select only 3 from the options';
    } else {
        for ($i = 0; $i < $numberSelected; $i  ) {
            $option1 = $checkBoxes[0];
            $option2 = $checkBoxes[1];
            $option3 = $checkBoxes[2];
        }
        echo 'You have selected', ' ', $option1, ' ', $option2, ' ', $option3;
    }  
}

CodePudding user response:

You can define a constant array with the allowed values, then only use values from that array when they correspond to the input value.

const ALLOWED_VALUES = [
    "apple"         => "Apple", 
    "samsung"       => "Samsung", 
    "lenovo"        => "Lenovo", 
    "google pixel"  => "Google Pixel",
];

if (isset($_POST['submit'])) {
    $checkBoxes = $_POST['myCheckBoxes'];
    $options = [];
    if (count($checkBoxes) > 3) {
        echo 'Please select only 3 from the options';
    } else {
        foreach($checkBoxes as $box) {
            $box = strtolower(trim($box));
            if(array_key_exists($box, ALLOWED_VALUES)){
                $options[] = ALLOWED_VALUES[$box];
            }
        }
        
        $option1 = (array_key_exists(0, $options))? $options[0]: null;
        $option2 = (array_key_exists(1, $options))? $options[1]: null;
        $option3 = (array_key_exists(2, $options))? $options[2]: null;
        
        echo 'You have selected', ' ', $option1, ' ', $option2, ' ', $option3;
    } 
}

The code above will accept "APPLE" but will use "Apple" anything not found or empty will be set to null. Run it live here: https://onlinephp.io/c/8409e

CodePudding user response:

checkboxes are not submitted when they are unchecked. so use the isset() function to check

  • Related