PHP Multiple Checkbox Array Handling

DISCLAIMER: This post may contain “affiliate” links to products and services I recommend. I’ll receive a small commission if you decide to purchase one of these products or services. I only recommend products I genuinely believe will help you in running your freelance business.

Handling Checkboxes in PHP Forms

I tend to get a lot of questions around checkbox handling when submitting forms to  PHP. Here’s a simple snippet to illustrate how it works:

A couple things to pay attention to here…

1. Notice the input names. They follow this pattern name[key]. The name is really the name of the “group” of checkboxes. The key is the name of that individual checkbox. This is how you know which checkboxes were checked and which weren’t. When submitted, this form will only include the checkboxes that ARE checked.

2. Notice the created array. When this form is submitted the output will look something like this:

Array ( [stuff] => Array ( [car] => 1 [dog] => 1 ) )

If you look at our inputs… “stuff” becomes the top-level array key, “dog” and “car” become the 2nd-level array keys, and the input values become the values of “dog” and “car” respectively.

With this, you can properly handle these checkboxes and process them accordingly.

You might also like

Do you want more freelance clients?

I’ll show you what I learned over the last 15 years to grind out (from absolute scratch) a backlog of new clients wanting to hire you. Who your best client prospect are, what services you should be offering them, where to find them and more. Just enter your email address in the box below and let’s get started:

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit
Share on pinterest
Pinterest
John Morris

JOHN MORRIS

I’m a 15-year veteran of freelance web development. I’ve worked with bestselling authors and average Joe’s next door. These days, I focus on helping other freelancers build their freelance business and their lifestyles.

This Post Has 9 Comments

  1. Hello – thanks for your article =)

    FYI – the input field labels don’t correctly correspond to the input name attributes (Dog is stuff[car]):
    Dog
    Car

  2. Hi John,

    is there any reason you choose this method over the following:

    Car
    Dog

    which results in the array:

    [stuff] => Array
    (
    [0] => Car
    [1] => Dog
    )

    just curious if it’s a matter of preference or if there are advantages to one method or the other?

    1. Yes actually. Having keys of 0, 1, 2, etc… don’t provide any meaning. If we were to look at it, we wouldn’t know what stuf[0] really means. But, stuff[car] would have some meaning built into it… and then we’d know the state to be 0 or 1… which is checked or unchecked. Technically, you could do it the other way… but it’s standard practice to do stuff[car] instead of stuff[0].

  3. Thanks a ton for your excellent tutelage, what fantastic help! Funny, was just watching your ‘php in 30 min’, needing this info-and there u are.

  4. Thanks a lot sir John!

    This article that you’ve made really solve an important problem in my project.
    Thanks a lot!
    God Bless ya sir!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do you want more freelance clients?

Enter your email below to get started building your system for consistently bringing in new freelance clients:

WHAT OTHERS ARE SAYING

Chris Aitken

He significantly improved my site through his expert knowledge of PHP, CSS and Javascript. Would definitely recommend John to others.

Sukh Plaha

John is a fantastic and patient tutor, who is not just able to share knowledge and communicate it very effectively – but able to support one in applying it. However, I believe that John has a very rare ability to go further than just imparting knowledge and showing one how to apply it. He is able to innately provoke one’s curiosity when explaining and demonstrating concepts, to the extent that one can explore and unravel their own learning journey. Thanks very much John!

Thabo Motsoahae

John is one of the best instructors I have come across, I learned a lot from his online tutorials.

Daniel Mohlendick

On the Freelancing on Upwork course: “This is by far the best course i have watched on Skillshare!! Thank you so much.”

Xan Barksdale

Xan Barksdale

Very professional worker who is extremely knowledgable in WordPress and Wishlist Member. I would definitely hire again.

Lewis Howes

John is amazing at building membership sites. He converted one of my sites over from it’s existing (hardly working) platform over to the clean and simple to use WishList membership platform. I highly recommend using John and WishList for any of your membership site needs.

Bob Patterson

Not only is John a very talented programmer and developer, he is also an excellent communicator. He has a talent for taking complex subjects and communicating them in terms that anyone can understand. This is a rare combination. This ability has enabled me to take my skills and knowledge to the next level. Thank you John for for all that you do.

Close Menu