0

I realize this is a question that has been asked before ("Cannot use [] for reading"), but I'm having trouble wrapping my head around the answer and how to fix my particular function.

function check_required_checkbox($checkbox_name, $error, $is_multiple_checkboxes)
{
    global $error_msgs;
    if ($is_multiple_checkboxes == true)
    {
        if (!isset($_POST[$checkbox_name][]))
        {
            $error_msgs[] = $error;
        }
    else if ($is_multiple_checkboxes == false)
    {
        if (!isset($_POST[$checkbox_name]))
        {
            $error_msgs[] = $error;
        }
    }
}

The problem line is 6, !isset($_POST[$checkbox_name][]), and I'm not understanding how the correct way I should write it. I saw instances of using brackets but !isset($_POST[{$checkbox_name}][]) isn't correct either.

When I have multiple checkboxes that use name="radda[]", I want my function to check that all of the checkboxes with a specific name are checked, and if not, add $error to the $error_msgs[] array.

EDIT: I discussed with the department that was requesting a rewrite of the old form. Instead of using checkboxes, I switched it to a list of all of the borrower's rights and responsibilities, and then used a radio button below the list to ask the user to select "yes" or "no" on whether they read the list. Then I made it required to select "yes" or "no" and added validation that if "no" was selected, they wouldn't be able to submit the application. This was far easier than trying to make a bunch of checkboxes required. I do appreciate the help that everyone offered though.

12
  • $checkbox name is defined? Try a print_r($_REQUEST); just to see all the variables sent in the request. Commented Aug 24, 2016 at 15:04
  • If you call a set of checkboxes name="radda[]" then you will recieve an array called $_POST['radda'] how many occurances depends upon how many of the checkboxes where actually checked. Unchecked checkboxes are not returned in the array Commented Aug 24, 2016 at 15:20
  • @RiggsFolly so in order to check that all of the checkboxes in a group are checked, I'd use if (isset($_POST['radda']) < 8)) { $error_msgs[] = $error; }? Commented Aug 25, 2016 at 13:18
  • No. Check if anything has been checked using if (isset($_POST['radda'])) check how may have been checked using if (count($_POST['radda']) < 8) { but remember there will only be an item in the array IF a checkbox is checked. If only 2 checkboxes are checked you will only get 2 items in the array Commented Aug 25, 2016 at 13:57
  • 1
    I do have an array created that has all of the borrower's rights and responsibilities, and then on the form creation, I just cycle through the array. I'd be able to use that array as the list of all the checkboxes, couldn't I? Commented Aug 25, 2016 at 19:26

1 Answer 1

1

If I understood what you need. Try to define the $checkbox_name variable and then access the POST.

$checkbox_name = 'radda';

if (isset($_POST[$checkbox_name]) && !is_array($_POST[$checkbox_name]))
{
   $error_msgs[] = $error;
}

Something like that should work.

Here is a similar thread: Retrieve an associative array value with a variable as key

Sign up to request clarification or add additional context in comments.

7 Comments

Not quite. My function was originally written that way, but it was only checking that ONE checkbox among a group was checked. I need the function to check that all checkboxes in a group are checked. Normally I could use $_POST['radda'][] (I think), and it'd work, but not if I'm passing a variable as the key?
what about checking if its an array? you can use is_array($_POST['radda']) after the isset but without the last []. I've just updated the answer with that condition.
$_POST is always an array though. Checking if it's an array is redundant.
Of course $_POST is an array, but the VARIABLE it contains can be an array or not, that's why is checking. Anyways what do you need? to check if a value of the post array is an array, right?
To check that an array of checkboxes from a form are all checked. I have a group of checkboxes on a form, which all have the same name, name="radda[]", and I need to make sure that all of them are checked because they're required. I know this can be validated in javascript, but I have to account for screen readers which most don't have javascript capabilities.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.