• 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
https://www.neowin.net/forum/topic/1171197-php-post-form-checkboxes-and-text/
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.

This topic is now closed to further replies.
  • Posts

    • Sony unveils its first fight stick 'Project Defiant', supports PC and PS5 by Pulasthi Ariyasinghe In a surprise twist, Sony announced a new piece of hardware at its State of Play presentation today. Currently going by the codename Project Defiant, it is Sony's first attempt at making a fight stick. Considering the company is currently partnered with Marvel to make a first-party fighting game featuring heroes and villains from the popular comic universe, this seems to be the right time to unveil the project. As seen in the teaser trailer above, the wireless fight stick follows the modern black and white PlayStation hardware style of the consoles and standard gamepad controllers. The controller will ship with a toolless design for changing the restrictor gates, with square, circle, and octagon options incoming for the stick. Meanwhile, the buttons are confirmed to have mechanical switches. Plus, a dedicated touchpad, much like on the DualSense controller, is available on the top of the fight stick. "It’s built in a sturdy, ergonomic design for a comfortable feel during intense fight sessions," adds the company. Sony is leveraging its PlayStation Link technology in the device. With it, the company is promising ultra-low latency wireless connectivity, making sure it provides players "precise in-game response to each button press and digital stick movement." Interestingly, PlayStation Link is Sony's proprietary wireless connectivity standard designed for PlayStation 5's audio transmissions, which is now being used a little differently for this piece of hardware. The controller has built-in storage for holding both the restrictor gates and the PS Link USB adapter. As expected, a wired connection is also possible, with a USB-C port available on the device for connecting to a PlayStation 5 or PC. To make sure the device remains in good condition during travel, Sony is throwing in a sturdy sling carry case with each Project Defiant fight stick. More details, like the official name, internal hardware, release date, and most importantly, price, will be revealed later. The hardware has a 2026 launch window right now.
    • Precisely. I didn't give Reddit permission to sell my posts to any AI vendor from any of my accounts. Since I've been on Reddit since before the current pseudo-AI existed, I don't think anyone did...
    • The character tech looks awful, like something from 10 years ago.
    • lol net-negative? They're worth $29 billion, so they aren't hurting. They're just money hungry and don't care at all about the free videos on their platform. They shouldn't be making money off of those, but they are. Those content creators that are uploading their videos they made aren't making anything, but YouTube is making tons of money off of them, along with being exorbitant. If they want to do what they did before and lock Premium videos in a paywall, then fine. But no one is going to put up with them strong-arming people into paying just to have an ad-free experience while watching super old videos uploaded by John Doe at 360p.
  • Recent Achievements

    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      348
    2. 2
      snowy owl
      167
    3. 3
      +FloatingFatMan
      164
    4. 4
      ATLien_0
      161
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!