tags:

views:

85

answers:

1

banging my head against the wall for something seemingly dead simple.

Here it is:

 <html>
<head></head>
<body>
<form method="post" action="action.php">

  <div><input type="checkbox"  name="test" value="Newspaper"> <span >Newspaper</span></div>
  <div><input type="checkbox"  name="test" value="PC"> <span >PC</span></div>
  <div><input type="checkbox"  name="test" value="Home"> <span >Home</span></div>
  <div><input type="checkbox"  name="test" value="Dont_know"> <span >dnunno</span></div>
    <input type="submit" name="Submit" value="send">
</form>

</body>
</html>

But when i select more then one option. I see in my print_r($_POST) statement only the last selected option in stead of all selected options. How should I deal with this?

update: I checked the rest of my code and i saw that this is done by some javascript.

else if(aform.validatorArr[i][4]=="checkbox"){
      var fvs="";
      eval("var chkbArray=aform."+aform.validatorArr[i][1]+";");
      if(aform.validatorArr[i][2] =="cb_true"){
       for (k=0;k<chkbArray.length;k++)
      {
        if (chkbArray[k].checked)
        {
        fvs+=chkbArray[k].value;
        console.log(fvs);
         }
        }
        if (fvs == false)
        {
        s+=aform.validatorArr[i][3]+"\n";
       }
       }
      }

thats why the [] is not added in my html. But how could I modify this code so that it will take all options?

+15  A: 

Put [] after the name:

<input type="checkbox"  name="test[]" value="Newspaper">

See PHP FAQ for more details.

karim79
Then you can foreach($_POST["test"] as $test) { ... }
Jonathan Sampson
thanks i updated my post.
sanders
@sanders - the code you posted is an excerpt, and it's really hard to understand without seeing the rest of what's happening. From what I can gather, the fvs variable is a concatenation of all values from the selected checkboxes, but I can't see where that's going.
karim79