array returns checked items only?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tfcRobert
Forum Newbie
Posts: 7
Joined: Tue May 16, 2006 5:12 pm
Location: Pasadena, CA

array returns checked items only?

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: array returns checked items only?

Post 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]}");
        ...
(#10850)
tfcRobert
Forum Newbie
Posts: 7
Joined: Tue May 16, 2006 5:12 pm
Location: Pasadena, CA

Post 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.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Re: array returns checked items only?

Post 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.
Post Reply