Passing checkboxes as an array in FORM &PHP


I have the following  HTML FORM with multi-values checkbox. It supposed to send all the boxes check as a list of values as some books said, to a PHP script. But it seem only send one value (the last one check or something), in the PHP it doen
t consider the checkbox variable as an array, it seems to be an C  character array that only has one value for one of the checked boxes.

 <form method = "post" action = "form2.php">

            <label>Site design
               <input name = "thingsliked[]" type = "checkbox"
                  value = "Design" /></label>
            <label>Links
               <input name = "thingsliked[]" type = "checkbox"
                  value = "Links" /></label>
            <label>Ease of use
               <input name = "thingsliked[]" type = "checkbox"
                  value = "Ease" /></label>
            <label>Images
               <input name = "thingsliked[]" type = "checkbox"
                  value = "Images" /></label>
            <label>Source code
               <input name = "thingsliked[]" type = "checkbox"
                  value = "Code" /></label>

  <input type = "submit" value = "Submit" />
 </form>



// The PHP scriipt
<?php

      extract($_POST);
       
       
       $boxes = $_POST['thingsliked'];

     

       

      for($i = 0; $i <=10; $i++)
      {
          echo "$i   $boxes[$i] ";

      }

?>



-- the output, with all the boxes checked

0 C 1 o 2 d 3 e 4 5 6 7 8 9 10
LVL 1
zzhang2006ProfessorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PanicXCommented:
Try inserting a key name into each array item, eg.
 <input name = "thingsliked[design]" type = "checkbox"
                  value = "Design" />

then in your php sycle through the array
foreach($boxes as $thingsliked){
echo "<br />" . $thingsliked;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Beverley PortlockCommented:
I would agree with the above comment, but I've always found it less error prone to use numbers for the array indexes rather than strings. You get the values when the box is ticked in any case.

So try converting your HTML to look like this

            <label>Site design
               <input name = "thingsliked[0]" type = "checkbox"
                  value = "Design" /></label>
            <label>Links
               <input name = "thingsliked[1]" type = "checkbox"
                  value = "Links" /></label>
            <label>Ease of use
               <input name = "thingsliked[2]" type = "checkbox"
                  value = "Ease" /></label>
            <label>Images
               <input name = "thingsliked[3]" type = "checkbox"
                  value = "Images" /></label>
            <label>Source code
               <input name = "thingsliked[4]" type = "checkbox"
                  value = "Code" /></label>

Then change your PHP code from this

       $boxes = $_POST['thingsliked'];      

      for($i = 0; $i <=10; $i++)
      {
          echo "$i   $boxes[$i] ";

      }


to this


      foreach( $_POST['thingsliked'] as $key => $value )
      {
          echo "$key   $value<br/> ";

      }



Only checkboxes that are ticked will show up. If you want to see UNTICKED boxes as well then try this technique - for every checkbox add a hidden field of the same name and give it a value that indicates it is unticked.

<input name = "thingsliked[1]" type = "hidden"     value = "unticked" />
<input name = "thingsliked[1]" type = "checkbox" value = "Links" />

The order of the two is important. The Hidden field MUST be declared FIRST.
0
Ray PaseurCommented:
Here is my teaching example that shows how to use checkboxes in an array, along with a few other tricks.  HTH, ~Ray
<?php // RAY_checkbox_array.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;


// DEMONSTRATE HOW TO SUBMIT CHECKBOXES IN THE FORM OF ARRAYS
// NOTE THAT THE UNCHECKED CHECKBOXES DO NOT COME THROUGH INTO $_POST
// NOTE THAT THE UNNAMED INPUT TAGS DO NOT COME THROUGH INTO $_POST


if (!empty($_POST["godo"]))
{
    // WHICH SUBMIT BUTTON FIRED?
    echo "YOU SUBMITTED {$_POST["godo"]}";
    echo PHP_EOL;
    var_dump($_POST);
}
// END OF PHP - USE HTML AND JAVASCRIPT TO PUT UP THE FORM
?>

<script language="JavaScript">
function toggle(formName, arrayName, YesNo)
{
   var cbox=document.getElementsByName(formName)[0].getElementsByTagName('input');
   for(i=0;i<cbox.length;i++)
   {
      if (cbox[i].name.substr(0,arrayName.length) == arrayName) cbox[i].checked=YesNo;
   }
}
</script>

<form method="post" name="checkForm">
GROUP A - SINGLE CLICK OR SELECT / DESELECT ALL
<input name="abox[1]" type="checkbox" value="A1" /> A1
<input name="abox[X]" type="checkbox" value="AX" /> AX
<input name="abox[3]" type="checkbox" value="A3" /> A3
<input                type="checkbox" onClick="toggle('checkForm', 'abox', this.checked)"> ALL OF 'A' (NO REFERENCE TO ANY OF 'B')

GROUP B - SINGLE CLICK OR SELECT / DESELECT ALL
<input name="bbox[1]" type="checkbox" /> B1
<input name="bbox[2]" type="checkbox" /> B2
<input name="allofB"  type="checkbox" onClick="toggle('checkForm', 'bbox', this.checked)"> ALL OF 'B' (NO REFERENCE TO ANY OF 'A')

GROUP C - SINGLE CLICK ONLY
<input name="cbox[]" type="checkbox" value="One" /> C[]
<input name="cbox[]" type="checkbox" value="Two" /> C[]
<input name="cbox[]" type="checkbox" value="Tre" /> C[]

SOME ADDITIONAL FORM DATA
<input name="thing"   type="text"    value="FOO" />
<input                type="text"    value="BAR" />
<input name="godo"    type="submit"  value="GO" />
<input name="godo"    type="submit"  value="DO" />
</form>

Open in new window

0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Ray PaseurCommented:
Oops, meant to include the link so you could see it in action.  Voila: http://www.laprbass.com/RAY_checkbox_array.php
0
zzhang2006ProfessorAuthor Commented:
Partially solved my problem

Thanks
0
Ray PaseurCommented:
Wow, you left this question for dead two months ago and now you marked the responses with a bad grade?  What in the world were you expecting?  Do you understand the grading system here at EE?  We are unpaid volunteers.  We can answer your questions but we cannot solve your problems or write your programming for you.  Please read the grading guidelines.
http://www.experts-exchange.com/help.jsp#hs=29&hi=403
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.