Sign in to follow this  
Followers 0

[PHP] Post form checkboxes and text


6 posts in this topic

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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)

Share this post


Link to post
Share on other sites

Posted

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

 

Share this post


Link to post
Share on other sites

Posted

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

 

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

Sorry for the double post my internet sux, and thanks ZakO

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.