Page 1 of 1

array returns checked items only?

Posted: Tue May 30, 2006 6:02 pm
by tfcRobert
I'm using an array of items in a form with a checkbox on each row of items to indicate that the row should be included upon submission of the form...

Code: Select all

while($row = mysql_fetch_array($deposits)){
        print("<div class=depositslip>");
        print("<INPUT TYPE=checkbox NAME=\"doInclude[]\" CHECKED>");
        print("<INPUT TYPE=hidden NAME=\"amount[]\" value=\"$row[0]\">");
        ...
My other arrays like amount[] are passed just fine but the doInclude[] array is passed with only the checked items and with keys that don't match up to the other arrays. For example, if two items out of many possible items are checked, print_r($doInclude) returns:

Array ( [0] => on [1] => on )

regardless of the rows that are checked, which is pretty useless.


The items are not in a table. The use of this method for returning arrays of variables from a form was suggested earlier in this forum and has proven usefull, but this has me stuck.

Any thoughts?

Rob

Re: array returns checked items only?

Posted: Tue May 30, 2006 6:08 pm
by Christopher
That's how checkboxes are supposed to work. Only the checked ones are sent. Maybe try:

Code: Select all

while($row = mysql_fetch_array($deposits)){
        print("<div class=depositslip>");
        print("<INPUT TYPE=\"checkbox\" NAME=\"amount[]\" value=\"{$row[0]}\" checked=\"checked\">{$row[0]}");
        ...

Posted: Tue May 30, 2006 7:22 pm
by tfcRobert
OK that helped. Now I'm placing the row index in the checkbox value so the returned array can be used to access the other arrays.

Code: Select all

$item = 0;
    while($row = mysql_fetch_array($deposits)){
        print("<div class=depositslip>");
        print("<INPUT TYPE=\"checkbox\" NAME=\"doInclude[]\" value=$item checked=\"checked\">");

And after selecting the first and third item I get:

Array ( [0] => 0 [1] => 2 )

Thanks for the help.

Re: array returns checked items only?

Posted: Tue May 30, 2006 11:11 pm
by harrisonad
arborint wrote:... Only the checked ones are sent. ...
yes it is true.
I usually do this when retrieving checkbox values:

Code: Select all

$terms_agreed = isset($_POST['checkbo_terms']);
Hope that helped.