Problem with checkboxes

    Date: 06/13/07 (PHP Community)    Keywords: php, html

    Hi all,

    I'm developing a little php script and am having problems with checkboxes. I'm doing all processing in one php file, and need to reprint the form with checkboxes checked if there are errors. I'm able to correctly pull which of the checkboxes were selected upon submission, but somehow when I print "checked=" (either true or false), they ALL get checked. The resulting HTML shows correctly, and I tested the HTML theory without the PHP so I'm thinking this is the correct way to do it, but it's not working and starting to tick me off...

    You can see what I mean in this script here:
    http://jen-m.com/php/testcheck.php



    $sp = $ _ POST['sports']; //spaces added for lj only
    $the_sports = array("baseball", "basketball", "football", "hockey");

    for ($i = 0; $i < count($the_sports); $i++)
    {
       $ck = (in_array($the_sports[$i], $sp)) ? "true" : "false";
       print("

  1. $the_sports[$i] ($ck)
  2. ");
    }



    ETA: So, it does work when I change it to print checked="checked" if true and nothing otherwise, but I'm still confused as to why my pure HTML (with checked true/false) worked and yet this code acts up. Maybe it was a problem on my HTML testing, but really, it worked...

    This one here clearly isn't the most sophisticated php script (although its actual intention will have a real use), but I would like to know (a) what I'm doing wrong in this instance, and (b) how the rest of you usually handle forms and checkboxes.

    Thanks for your help!

    Source: http://community.livejournal.com/php/570711.html

« JS Problem || PHP issue on Windows server »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home