• 0

[PHP] Post form checkboxes and text


Question

I have this html form:

 
<form action="" method="POST">
0<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
1<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
2<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
3<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
4<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
5<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
6<input type="text" name="name-txt[]" /><input type="checkbox" name="chbox[]"/><br/>
<input type="submit" name="submit" value="submit"/>
</form>

PHP CODE:

<?php
if(isset($_POST['submit'])){
$txt = $_POST['name-txt'];
$chk = $_POST['chbox'];


print_r($chk);

}

?>

i want to list all check boxes checked and unchecked like this
if i checkbox 1
will returns me 
array(
      [0] => on
)

but when i checkbox 5 will return me the same array key[0] => on so how can i do it like this (like text fields):
if i checkbox 2,3,6

array(
      [0] => 
      [1] => 
      [2] => on
      [3] => on
      [4] =>
      [5] =>
      [6] => on
)

thanks alot.

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Checkboxes that are unchecked never get sent to the server, a common (albeit ugly) way to workaround this is to add an additional hidden control with the same name containing a default value, if the checkbox is ticked, that value will be overwritten, i.e:
 

<form action="" method="POST">
0<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[0]" value="" /><input type="checkbox" name="chbox[0]" /><br/>
1<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[1]" value="" /><input type="checkbox" name="chbox[1]"/><br/>
2<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[2]" value="" /><input type="checkbox" name="chbox[2]"/><br/>
3<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[3]" value="" /><input type="checkbox" name="chbox[3]"/><br/>
4<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[4]" value="" /><input type="checkbox" name="chbox[4]"/><br/>
5<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[5]" value="" /><input type="checkbox" name="chbox[5]"/><br/>
6<input type="text" name="name-txt[]" /><input type="hidden" name="chbox[6]" value="" /><input type="checkbox" name="chbox[6]"/><br/>
<input type="submit" name="submit" value="submit"/>
</form>

Then print_r($_POST['chbox']) will give, for example:

Array
(
  [0] =>
  [1] =>
  [2] =>
  [3] =>
  [4] => on
  [5] => on
  [6] =>
)

When doing this you need to add explicit indices as I've done in the example above, you can't just use chbox[]. 

 

There are various other ways to approach this depending on your use-case though, for example, give each checkbox a unique known value, and just check if (in_array(id, $_POST['chbox'])) to determine if it was checked.

Link to comment
Share on other sites

  • 0

You could try this:

foreach($_POST['chbox'] as $boxes){
    echo $boxes . ' ';
}

it'll loop through all checkboxes and show their value (0 or 1 I think)

Link to comment
Share on other sites

  • 0

You could try this:

foreach($_POST['chbox'] as $boxes){
    echo $boxes . ' ';
}

it'll loop through all checkboxes and show their value (0 or 1 I think)

It's not that i want...

checkboxes.jpg

 

Link to comment
Share on other sites

  • 0

You could try this:

foreach($_POST['chbox'] as $boxes){
    echo $boxes . ' ';
}

it'll loop through all checkboxes and show their value (0 or 1 I think)

It's not that i want...

checkboxes.jpg

 

Link to comment
Share on other sites

This topic is now closed to further replies.