0
votes

I have the following inputs field in my form:

    <label class="addFileSectionSelectArea">
        <span class="addFileSectionSelectAreaText">לבנון</span>
        <input type="checkbox" name="add_file[areas]" value="lebanon">
        <div>
            <svg viewBox="0 0 44 44">
                <path d="M14,24 L21,31 L39.7428882,11.5937758 C35.2809627,6.53125861 30.0333333,4 24,4 C12.95,4 4,12.95 4,24 C4,35.05 12.95,44 24,44 C35.05,44 44,35.05 44,24 C44,19.3 42.5809627,15.1645919 39.7428882,11.5937758" transform="translate(-2.000000, -2.000000)"></path>
            </svg>
        </div>
    </label>
    <label class="addFileSectionSelectArea">
        <span class="addFileSectionSelectAreaText">סוריה</span>
        <input type="checkbox" name="add_file[areas]" value="syria">
        <div>
            <svg viewBox="0 0 44 44">
                <path d="M14,24 L21,31 L39.7428882,11.5937758 C35.2809627,6.53125861 30.0333333,4 24,4 C12.95,4 4,12.95 4,24 C4,35.05 12.95,44 24,44 C35.05,44 44,35.05 44,24 C44,19.3 42.5809627,15.1645919 39.7428882,11.5937758" transform="translate(-2.000000, -2.000000)"></path>
            </svg>
        </div>
    </label>

In PHP, I try to retrieve (after submit) the checked checkboxes:

        $debug = $_POST['add_file']['areas'];

When I do: var_dump($debug) it always returns string value which represents the value of the last selected checkboxes (however I expect it to return an array of all selected checkboxes).

So, for example, if I select both [value='syria'] and [value='lebanon'] checkboxes, then I only get a string with value 'syria' in the var_dump (because syria input is after lebanon input in the html). Why is that?

1
Do name="add_file[areas][]" to make it an array of values. Although unless there is a reason, I'd just go with name="add_file_areas[]" and access it in $_POST['add_file_areas']. - IncredibleHat

1 Answers

1
votes

You need to end the names with an empty array syntax [] to append another element for a checkbox group, so either:

 <input type="checkbox" name="add_file[]" value="syria">

Or if the areas array part is important for some reason:

 <input type="checkbox" name="add_file[areas][]" value="syria">

There is a good guide on this specific thing here: Handling checkbox in a PHP form processor