• 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

    • Microsoft is removing legacy drivers from Windows Update by Usama Jawad Last month, we learned that Microsoft is making major changes to the development of hardware drivers in Windows. This included the retirement of Windows Metadata and Internet Services (WMIS), along with the process for pre-production driver signing. Now, the Redmond tech firm has informed partners that it will be getting rid of old drivers in Windows Update. In what is being described as a "strategic" move to improve the security posture and compatibility of Windows, Microsoft has announced that it will be performing a cleanup of legacy drivers that are still being delivered through Windows Update. Right now, the first phase only targets drivers that already have modern replacements present in Windows Update. As a part of its cleanup process, Microsoft will expire legacy drivers so that it is not offered to any system. This expiration involves removing audience segments in the Hardware Development Center. Partners can still republish a driver that was deemed as legacy by Microsoft, but the firm may require a justification. Once the Redmond tech giant completes its first phase of this cleanup, it will give partners a six-month grace period to share any concerns. However, if no concerns are brought forward, the drivers will be permanently eradicated from Windows Update. Microsoft has emphasized that this will be a regular activity moving forward and while the current phase only targets legacy drivers with newer replacements, the next phases may expand the scope of this cleanup and remove other drivers too. That said, each time the company takes a step in this direction, it will inform partners so that there is transparency between both parties. Microsoft believes that this move will help improve the security posture of Windows and ensure that an optimized set of drivers is offered to end-users. The firm has asked partners to review their drivers in Hardware Program so that there are no unexpected surprises during this cleanup process.
    • No idea, but I had a client the other week that lost the entire drive to it. I suggested relying on the Samsung T7's instead. The Sandisk Extreme's had reliability issues too.
    • I use it every day so personally yes I need it, or rather I want it. I use OpenShell though, not the garbage modern Start Menu. I just counted and at the moment I have a total of 92 program shortcuts organized into six folders almost exactly the way I did back in Windows 95. I can get to any program I want to run very quickly. I never use Search to find or run programs.
    • I do miss the Apps view from Windows 8.1 Update.
    • I use the search function and little else since Windows 11 (but there are times where Saved Searches take precedence since the search feature on the Start menu is worse than before). I use other features in previous releases.
  • Recent Achievements

    • One Month Later
      gowtham07 earned a badge
      One Month Later
    • Collaborator
      lethalman went up a rank
      Collaborator
    • Week One Done
      Wayne Robinson earned a badge
      Week One Done
    • One Month Later
      Karan Khanna earned a badge
      One Month Later
    • Week One Done
      Karan Khanna earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      665
    2. 2
      ATLien_0
      263
    3. 3
      Michael Scrip
      212
    4. 4
      +FloatingFatMan
      168
    5. 5
      Steven P.
      156
  • Tell a friend

    Love Neowin? Tell a friend!