-2

I am having trouble with form validation for checkboxes.

Here is the checkbox code:

<div class="form-group">
    <div class="checkbox checkbox-danger checkbox-inline">
        <input type="checkbox" name="defect[]" value="{$ng_id}">
    </div>
</div>

If I just do something like this then it echo's out whatever I select.

foreach($_POST['defect'] as $defects) {
echo $defects;
}

I have tried to use the empty function on its own as well as this:

if(empty($defects) || count($defects) < 1) {

but I just keep getting the error:

Undefined index: defect

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Iggy's Pop
  • 589
  • 1
  • 6
  • 24
  • 2
    `$defects=$_POST['defect']; foreach($defects as $defect){ ...` this is the correct way to access the array contained inside the `$_POST['defect']` – Lelio Faieta Mar 20 '17 at 14:37
  • checkboxes don't use `empty()`, they use `isset()`. – Funk Forty Niner Mar 20 '17 at 15:00
  • and where's the form for this or how are you handling "POST"? – Funk Forty Niner Mar 20 '17 at 15:00
  • `value="{$ng_id}"` that seems to be angular-related. – Funk Forty Niner Mar 20 '17 at 15:01
  • well, if you left the question, you'll either have to ping one of us, or see the answers given. – Funk Forty Niner Mar 20 '17 at 15:05
  • @ Fred, no it isn't angular. It is the way I put php variables into code using heredoc. – Iggy's Pop Mar 20 '17 at 15:06
  • @Jonathan you didn't ping correctly (as I did for you here); my member name is distinctive. I only saw this because I still had the tab opened (by accident). well, you have more code than you posted. Again, see the answers below. I have my own thoughts on this, but seeing that I don't know which animal(s) we're really dealing with here, I am hesitant to keep going or posting an answer. Good luck, sincerely. – Funk Forty Niner Mar 20 '17 at 15:09

2 Answers2

-1

Problem is that when you do not select any of checkboxes, no variable named defect is sent. Simply create hidden field before any checkbox with empty value to always send it.

<input type="hidden" name="defect[]" value="">

And in PHP side:

if (!empty($_POST['defect']) && count($_POST['defect']) > 1) {
    // all ok
} else {
    // show error
}
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • Unfortunately this code doesn't take into account that $_POST['defect'] might not be an array. Applying count a variable that is not an array will return TRUE. The user would then assume they can loop through an array that isn't there and get a "Invalid argument supplied for foreach() in" warning. In addition, the user requests that at least 1 checkbox needs to be checked yet the code above says there must be more than one checked. – Watts Epherson Mar 20 '17 at 14:55
-2

A posted array is usually not set if nothing was ticked.

You can do the following check if you want to also check it's an array:-

if(@!is_array($_POST['defect'])){
    // Error
} else {
    foreach($_POST['defect']){
        // do stuff
    }
}

This is suppressing the warning using @ otherwise you would get the "not defined" error.

If you want to avoid that you could just do:

if(!isset($_POST['defect'])){
   // Now check it's an array and has a count > 0
}

.. but in addition you still need to ensure it's an array and has a count greater than zero. The advantage with suppressing the warning in this specific instance is that you know it's an array PLUS you know there must be a value in it otherwise no array would have been passed in from the POST. It's one of those rare moments where suppressing the warning saves you doing 2 additional IF checks to avoid other warnings.You HAVE to check it's an array otherwise you will get the "invalid foreach" warning...

Watts Epherson
  • 692
  • 5
  • 9
  • Adding a comment to this as I noticed it gets down voted. To explain the reason it has been down voted - some people don't approve of the @ symbol as it is like ignoring a warning exists - even though in this instance I don't need the warning because I know what the warning will be; it's not jumping out by surprise. However, we still don't want a log file filled up somewhere with warnings in. Anyway, the second example does explain how to do it without surpressing the warning... – Watts Epherson Aug 21 '18 at 14:12