#1
  1. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,619
    Rep Power
    595

    Trouble with Checkboxes


    This is very bizarre. I have a form with an array of checkboxes. Some are initialized as checked and others are not. I test the checkboxes like this:
    PHP Code:
    $paid=$_POST['paid'];
    $member=$_POST['member'];
    for (
    $i=0$i<count($member); $i++) {
        if (isset(
    $paid[$i])) {
            echo 
    "$paid[$i]<br />\n";
        }

    It is the 3rd element that is giving me trouble (if it matters the previous 2 are checked and behave as expected). Before the submit, the HMTL for that checkbox is:
    Code:
    <input type="checkbox" name="paid[]" />
    When the submit is processed this element is not only defined but has a value of 'on'. None of the other unchecked checkboxes behave like that. The are all unset as expected. Any ideas?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124
    what's in the html?

    A checkbox should also have a value,

    eg

    Code:
    <input type='checkbox' name='paid' value='yes' />
    <input type='checkbox' name='paid' value='in part' />
    I'm guessing that your browser is sending "on" as the default value when the box is checked but has no value

    Also, what is in $paid[$i] and how do you make $paid[$i]?
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    this could be a problem of the auto generated indices, which can easily lead to a wrong order. Try explicit indices in your HTML: paid[0], paid[1], ...

    Comments on this post

    • Northie agrees
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,619
    Rep Power
    595
    Thanks for the replies. I don't really care what the value is, all I care about is if it is checked. Since the HTML clearly shows it is not checked, it should be unset but is not. As for auto-indices, I'll give that a try.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124
    given that unchecked checkboxes don't get sent, then you maybe incorrectly assuming that the checkboxes you see after submit are the same as the ones before submit.

    Setting your own array keys (indices, as already mentioned) will help
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,619
    Rep Power
    595
    And it did. Explicit indices fixed the problem. Thanks.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo