Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6770175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:16:51+00:00 2026-05-26T15:16:51+00:00

Having trouble getting my POST arrays to show all checkbox values from my form.

  • 0

Having trouble getting my POST arrays to show all checkbox values from my form.

I have a form set up as follows:

<form name='foo' method='post' action=''>
    <table>
       <tr>
          <td class='bla'>Checkbox: <input type='checkbox' name='cBox[]'/></td>
      </tr>
       <tr>
          <td class='bla'>Checkbox: <input type='checkbox' name='cBox[]'/></td>
      </tr>
       <tr>
          <td class='bla'>Checkbox: <input type='checkbox' name='cBox[]'/></td>
      </tr>
   </table>
</form>

I have a button at the bottom bound to a jquery function that adds 5 more empty rows to the form (hence the arrays for the input name cBox[]).

Now, the problem. Lets say the first checkbox is unchecked, and the last 2 are checked. When I output the values (using PHP print_r for debugging), I will get:

Array ( [0] => on [1] => on)

For some reason, the array does not contain any value for unchecked checkboxes.

I have seen some solutions where a hidden variable is passed with each checkbox, but can this solution be implemented in my situation (using arrays)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:16:52+00:00Added an answer on May 26, 2026 at 3:16 pm

    That behavior is not surprising, as the browser doesn’t submit any value for checkboxes that are unchecked.

    If you are in a situation where you need to submit an exact number of elements as an array, why don’t you do the same thing you do when there’s an id of some sort associated with each checkbox? Just include the PHP array key name as part of the <input> element’s name:

      <tr>
                                                           <!-- NOTE [0] --->
          <td class='bla'>Checkbox: <input type='checkbox' name='cBox[0]'/></td>
      </tr>
       <tr>
          <td class='bla'>Checkbox: <input type='checkbox' name='cBox[1]'/></td>
      </tr>
       <tr>
          <td class='bla'>Checkbox: <input type='checkbox' name='cBox[2]'/></td>
      </tr>
    

    That still leaves you with the problem that unchecked boxes will still not be present in the array. That may or may not be a problem. For one, you may really not care:

    foreach($incoming as $key => $value) {
        // if the first $key is 1, do you care that you will never see 0?
    }
    

    Even if you do care, you can easily correct the problem. Two straightforward approaches here. One, just do the hidden input element trick:

      <tr>
          <td class='bla'>
            <input type="hidden" name="cBox[0]" value="" />
            Checkbox: <input type='checkbox' name='cBox[0]'/>
          </td>
      </tr>
       <tr>
          <td class='bla'>
            <input type="hidden" name="cBox[1]" value="" />
            Checkbox: <input type='checkbox' name='cBox[1]'/>
          </td>
      </tr>
    

    And two, which I find preferable, fill in the blanks from PHP instead:

    // assume this is what comes in:
    $input = array(
        '1' => 'foo',
        '3' => 'bar',
    );
    
    // set defaults: array with keys 0-4 all set to empty string
    $defaults = array_fill(0, 5, '');
    
    $input = $input + $defaults;
    print_r($input);
    
    // If you also want order, sort:
    
    ksort($input);
    print_r($input);
    

    See it in action.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble getting my CSS to work :( HTML <form action=/api/submit/ method=post> <textarea
I am having trouble getting the page to work. I have my form method
i'm having trouble getting the date to be imported into mysql from my form.
I am having trouble getting an android POST of a simple JSONObject to show
I'm having trouble getting a for to submit from my show.html.erb page. It is
I'm using express and having trouble getting form data from the bodyParser. No matter
I'm having trouble getting a response back from a Jquery ajax call... (It's a
Having trouble getting my JQuery POST to be accepted by the WCF Service. Here's
I am having trouble getting dynamic content coming from a custom handler to be
Im new to PHP and Im having trouble getting a contact from to submit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.