Toniq
Toniq

Reputation: 5006

PHP form multiple checkbox groups

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>

Upvotes: 0

Views: 1252

Answers (1)

Scuzzy
Scuzzy

Reputation: 12332

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>

Upvotes: 1

Related Questions