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 8715837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:56:30+00:00 2026-06-13T05:56:30+00:00

I have a form with multiple checkboxes, which I want to put into an

  • 0

I have a form with multiple checkboxes, which I want to put into an array. I go by the example provided here: http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html

So I’ve prepared the files:

checkboxes.php:

<form action="checkboxes2.php" method="post">
<input type="checkbox" name="tags[]" value="1" />1<br>
<input type="checkbox" name="tags[]" value="2" />2<br>
<input type="checkbox" name="tags[]" value="3" />3<br>
<input type="checkbox" name="tags[]" value="4" />4<br>
<input type="submit" value="Send" />
</form>

checkboxes2.php:

<?php
print_r($_POST["tags"]);
?>

Pretty simple…I realize I should only get the value of these textboxes and not if they have been selected or not. But I still get this error:

Undefined index: tags in checkboxes2.php on line 2

I have absolutely no idea what I did wrong here. I went by the example in the link above and did everything exactly the same (mainly copy/pasting and changing some parts like adding a submit button) but I don’t get the output as shown in the example. I should at least get the values of each of these checkboxes, right?

What I want to do: I want to check the array of checkboxes, see which ones have been selected and add “yes” or “no” into a second array, like this:

<?php
  $number1 = $_POST["tags"];
  $number2 = array();

  foreach($number1 as $number1_output)
  {
    if(isset($number1_output))
    {
      $number2[] = "yes";
    }
    else
    {
      $number2[] = "no";
    }
  }

  print_r($number2);
?>

Well…it only half works. Only the checkboxes that have been selected are added to the array. So if I select “3” and “4” I get this:

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

What is the best way to deal with checkboxes in arrays and validating them?

  • 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-06-13T05:56:32+00:00Added an answer on June 13, 2026 at 5:56 am

    Alternatively, if you want to have checkboxes that also send a value if they are not checked, you can add a Hidden input field before the checkbox:

    <input type="hidden" name="tag[1]" value="off" />
    <input type="checkbox" name="tag[1]" value="on />
    

    Because the checkbox is only sent if it is set, but it overrides the hidden as it is later in the HTML, you will now always have tag[1] set with either ‘off’ or ‘on’ depending on whether or not the box was checked.

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

Sidebar

Related Questions

I have multiple checkboxes in CakePHP's Add/Edit view, created with: echo $this->Form->input('email_warning_chb', array('type'=>'select', 'multiple'=>'checkbox',
I have a data entry form on my site which contains multiple checkboxes, two
I have a table with multiple rows which contain form inputs (checkboxes, text, dropdowns).
I have multiple checkboxes in my form and i want to use those to
I have inserted this form with multiple checkbox items which collects some values, What
I have form with multiple input type=radio with text next to them. I want
I have a form with multiple textboxes which are created dynamically, now all these
I have a form made in JSP, here I have multiple buttons - Approve,
I have a form in which a user can select multiple options in a
I have multiple checkboxes in a form. Based on clicking those checkboxes, I show

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.