ke-jo Posted September 25, 2020 Share Posted September 25, 2020 Hello, I am trying phpfreaks out without much luck here. If anybody here truly knows how to validate checkboxes as a group please give some assistance. I had to remove the array from the 'name' attribute in my checkbox inputs in order to get the user's checked selections to be properly distributed into separate database columns (not all in one column as a string of text). I am now able to successfully get the entries into the database as "1" for selected and "0" for un-selected. Now however, since there is no array that is holding these input names my validation class method does not work. My validation is suppose to show an error if none of these checkboxes are selected. I had to change each input name to its own identity ( fruit_selection_apple, etc... ), then set my validation like (see code below). However, this is NOT OOP PHP. I had to hard code these values into the validation class. I am also now getting a notice, " Notice: Undefined index: fruit_selection ". Does anyone legitimately know their PHP enough to help with this issue? public function check($source, $items = array()) { foreach($items as $item => $rules) { foreach($rules as $rule => $rule_value) { $value = trim($source[$item]); $item = escape($item); $checkboxvalue = (isset($_POST['fruit_selection_apple'])) || (isset($_POST['fruit_selection_orange'])) || (isset($_POST['fruit_selection_banana'])) || (isset($_POST['fruit_selection_pear'])) || (isset($_POST['fruit_selection_kiwi']) ) if($rule === 'atleastone' && empty($checkboxvalue)) { $this->addError("{$item} You must select at least one checkbox."); } Quote Link to comment https://forums.phpfreaks.com/topic/311523-checkbox-insertvalidation-problem/ Share on other sites More sharing options...
gw1500se Posted September 25, 2020 Share Posted September 25, 2020 '$checkboxvalue' will never be 'empty'. It is a boolean value. If you want to know if none of the boxes are checked then use '!$checkboxvalue' in the 'if' statement. Quote Link to comment https://forums.phpfreaks.com/topic/311523-checkbox-insertvalidation-problem/#findComment-1581577 Share on other sites More sharing options...
Barand Posted September 25, 2020 Share Posted September 25, 2020 Try running the script below. It will display the posted form input. If you see what is being posted it should give you a clue about how to process it. <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { // was form data submitted? if (isset($_POST['fruit'])) { echo "Your form inputs were<br>"; echo '<pre>$_POST = ', print_r($_POST, 1), '</pre>'; echo "<hr>"; } else echo "No fruits were selected<hr>"; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Example</title> <style type='text/css'> label { display: inline-block; width: 80px; } </style> </head> <body> <form method="POST"> <fieldset> <legend>Select fruits</legend> <label for='apple'>Apple</label> <input type="checkbox" name="fruit['apple']" id="apple" value="1"> <br> <label for='orange'>Orange</label> <input type="checkbox" name="fruit['orange']" id="orange" value="1"> <br> <label for='banana'>Banana</label> <input type="checkbox" name="fruit['banana']" id="banana" value="1"> <br> <label for='pear'>Pear</label> <input type="checkbox" name="fruit['pear']" id="pear" value="1"> <br> <label for='kiwi'>Kiwi</label> <input type="checkbox" name="fruit['kiwi']" id="kiwi" value="1"> <br> </fieldset> <br> <input type="submit" value="Submit"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/311523-checkbox-insertvalidation-problem/#findComment-1581578 Share on other sites More sharing options...
kicken Posted September 26, 2020 Share Posted September 26, 2020 7 hours ago, ke-jo said: Does anyone legitimately know their PHP enough to help with this issue? Yes. You're issue isn't that the people here don't know what they are doing, the problem is you keep changing things and understanding how it affects the code. The code you had in your original post in the original thread was fairly close to correct. Requinix said what you needed to do to fix it (by saying what you need to do, not just giving you the code to do it) but I guess you didn't understand or something. Then you changed the code to something else which invalidated that advice and started you down a different path. So let's just back the f* up and go back to that original code with your checkboxes as a simple array. <input type="checkbox" name="fruit_selection[]" value="fruit_apple"> <input type="checkbox" name="fruit_selection[]" value="fruit_orange"> <input type="checkbox" name="fruit_selection[]" value="fruit_banana"> <input type="checkbox" name="fruit_selection[]" value="fruit_pear"> <input type="checkbox" name="fruit_selection[]" value="fruit_kiwi"> What this will do is create an array when submitted called $_POST['fruit_selection'] The contents of this array will be the choices that the user selected. The items that were not selected will not exist in the array. Your original code seems to indicate you almost understood this as you were checking for $_POST['fruit_selection'][0] and so on. What you seemingly failed to understand is that element [0] in that array does not equate to the checkbox with "fruit_apple", it equates to whichever is the the first checkbox the user actually checked is (which could be fruit_kiwi for example if they only checked the last one). So what you want to do to get your 1/0 values it not check of those positions in the array exist, but check if the values of your checkboxes exist within that array. This is what requinix was telling you to do. One way to do that is using php's in_array function. For example. $isKiwiChecked = in_array('fruit_kiwi', $_POST['fruit_selection']); Now you should have enough information to figure everything out. So take a time out and spend some time learning rather than blaming everything on the people attempting to help you being idiots. Use things like var_dump as demonstrated above to get an idea of what data you have available and what format it's in so you can better learn what to do. Quote Link to comment https://forums.phpfreaks.com/topic/311523-checkbox-insertvalidation-problem/#findComment-1581585 Share on other sites More sharing options...
ke-jo Posted September 26, 2020 Author Share Posted September 26, 2020 @Barand Thank you for your reply. I think I can see what is going on. I will work with your code. Would you be offended by a $100usd donation? Are you available for continued support? Quote Link to comment https://forums.phpfreaks.com/topic/311523-checkbox-insertvalidation-problem/#findComment-1581592 Share on other sites More sharing options...
Barand Posted September 26, 2020 Share Posted September 26, 2020 Correction to my above code <input type="checkbox" name="fruit['apple']" id="apple" value="1"> ^ ^ | | remove quotes in input names Quote Link to comment https://forums.phpfreaks.com/topic/311523-checkbox-insertvalidation-problem/#findComment-1581594 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.