0

I have a group of checkboxes in a from and I need to get values with php. Note that div class="group" can also be duplicated onruntime (with jquery) so I may end with multiple div class="group" of checkboxes.

Is there a way to set checkboxes name so form data gets grouped together for each "group", or whatever is easier to handles afterwards? There is also a problem of checkboxes that were not checked, so they wont be present in a post data.

I tried using names like this but I dont like the results.

<form>
    <div class="group">
        <input type="checkbox" name="ev[][time]">
        <input type="checkbox" name="ev[][space]">                 
        <input type="checkbox" name="ev[][money]">
    </div>

    <div class="group">
        <input type="checkbox" name="ev[][time]">
        <input type="checkbox" name="ev[][space]">                 
        <input type="checkbox" name="ev[][money]">
    </div>
</form>
4
  • depends how you want to handle the data server side. ev[time][] could be better could be worse, no way to know currently Commented Jun 10, 2018 at 21:13
  • Specify the index, ev[0][time], ev[1][time] etc should be used, because a un-ticked checkbox's "empty" or "false" state will not be sent, messing up your array indexing. Basically use row[0][key] notation for simplest grouping Commented Jun 10, 2018 at 21:19
  • What's wrong with the result that you don't like it? I would use ev1... for the first group and ev2... for the second group. Commented Jun 10, 2018 at 21:23
  • If you had three sets of groups like above and ticked the first and third "time" checkbox skipping the middle one you would get ev[0][time] + ev[1][time] and not ev[0][time] + ev[2][time] when using the [] notation with checkbox fields. Much safer to be verbose with field names when grouping. Commented Jun 10, 2018 at 21:24

1 Answer 1

1

When defining checkbox groups its much better to be verbose with input names and avoid using the [] notation.

A un-ticked checkbox is not sent in post data, and your selections will become misaligned.

<form>
    <div class="group">
        <input type="checkbox" name="ev[0][time]">
        <input type="checkbox" name="ev[0][space]">                 
        <input type="checkbox" name="ev[0][money]">
    </div>
    <div class="group">
        <input type="checkbox" name="ev[1][time]">
        <input type="checkbox" name="ev[1][space]">                 
        <input type="checkbox" name="ev[1][money]">
    </div>
    <div class="group">
        <input type="checkbox" name="ev[2][time]">
        <input type="checkbox" name="ev[2][space]">                 
        <input type="checkbox" name="ev[2][money]">
    </div>
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

I still dont get values in post for unchecked checkboxes.
And you never will, because that's how post data up works with checkboxes. What this code does is avoid any conflicting information by setting a unique index per group. The problem is if you don't check any boxes and submit the form you will not get the ev data at all, if you at least check the very last checkbox you will get $_POST['ev'][2]['money'] and thus can iterate over $_POST['ev']. Fill in the gaps with your PHP code, you have no choice but to work around this "limitation" yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.