Many Cheboxes

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
pentium3id
Forum Newbie
Posts: 1
Joined: Tue Jul 23, 2002 12:28 pm
Location: Portugal, Lisbon

Many Cheboxes

Post by pentium3id »

Hi all,
this is my first post here, cause I have a problem, :x

The best way to describe it is to use the file analogy.
Imagine two columns, the 1st with filenames, the second
with a checkbox. If I want to delete a certain file, I click
the checkbox next to it.

When the user presses Submit, I have to go checkbox
by checkbox to see if it is selected, and if it is, delete the
acording file.

Now, how do I handle a dynamic number of checkboxes?
With an array?

If someone could post a link to a tutorial on large forms
handling, I would be much grateful.

Thanx all
User avatar
musashi
Forum Commoner
Posts: 39
Joined: Tue Jul 23, 2002 12:51 pm
Location: Santa Cruz - CA

Handling large lists

Post by musashi »

I do not know of a tutorial, but I think I can explain how to do this (I've done it before myself).

You are right, an array is needed here. Try, in the input tag, using an array:

<input type="checkbox" value="$filename" name="fileList[]">

where $filename is the name of the file (or id) that the checkbox represents. When this is submited, php will receive a variable called $fileList which will be an associative array of all of the $filename that were checked off.
User avatar
dlgilbert
Forum Newbie
Posts: 6
Joined: Wed Jun 19, 2002 7:03 am
Location: Pennsylvania, USA
Contact:

Excellent!

Post by dlgilbert »

Good tip! I've always wondered how to do that in an "un-kludgy" way. :-)
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Concerning this

<input type="checkbox" value="$filename" name="fileList[]">

How would you deal with this on the page it is submited to. Lets just print it out to the screen for ease of argument.

I have a list of users I want to check off. So it calls up all the users. The $filename in this case is $user.
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post by samscripts »

How would you deal with this on the page it is submited to. Lets just print it out to the screen for ease of argument
Ok, this script submits to itself...

Code: Select all

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Who are the users&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php

if( isset($_POST&#1111;'submit'] ) ){   // if the form has been submitted

?&gt;
&lt;h4&gt;The users are:&lt;/h4&gt;
&lt;?php

   if( isset($_POST&#1111;"users"]) ){  // check to see if any checkboxes selected

      $users = $_POST&#1111;"users"];  // get the array of selected values
      $cnt = count($users);  // count the number of selected items

      for( $i = 0; $i &lt; $cnt; $i++){
         echo $users&#1111;$i], ' is a user!&lt;br&gt;';  // display each checked item
      }

   }else{

      echo "There are no users!";  // no checkboxes checked

   }

}else{ // the form has not been submitted yet, so display it

?&gt;
&lt;h4&gt;Who are the users?&lt;/h4&gt;
&lt;form action="testcheckboxes.php" method="post"&gt;
&lt;input type="checkbox" name="users&#1111;]" value="George"&gt;George&lt;br&gt;
&lt;input type="checkbox" name="users&#1111;]" value="Tony"&gt;Tony&lt;br&gt;
&lt;input type="checkbox" name="users&#1111;]" value="Saddam"&gt;Saddam&lt;br&gt;
&lt;input type="checkbox" name="users&#1111;]" value="Vladimir"&gt;Vladimir&lt;br&gt;
&lt;input type="submit" name="submit" value="Submit"&gt;
&lt;/form&gt;
&lt;?php

}

?&gt;
&lt;/body&gt;
&lt;/html&gt;
hope this helps,

Sam
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Thank you so much. I have been struggleing with this for over a week and with the this and what I have seen in other spots on this forum I now have what i needed.

Thsi forum is great. I plan on making it a daily stop for me.

Oldtimer
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

In putting this to my pages I have found a problem. I have this on my form page
<hr>
$i=0;
while ($i < $num) {
$guser=mysql_result($result,$i,'guser');
$guser=htmlspecialchars($guser);
echo "<input type=\"checkbox\" name=player[$i] value=\"".$guser."\">$guser\n"; echo "<BR>";
++$i;
}
<hr>
Which Displayed this on the site.
<input type="checkbox" name=player[0] value="number1">number1
<BR><input type="checkbox" name=player[1] value="number3">number3
<BR><input type="checkbox" name=player[2] value="number4">number4
<BR><input type="checkbox" name=player[3] value="number5">number5
<BR><input type="checkbox" name=player[4] value="number6">number6
<BR><input type="checkbox" name=player[5] value="john2">john2
<BR><input type="checkbox" name=player[6] value="mike">mike
<BR><input type="checkbox" name=player[7] value="henry">henry
<BR><input type="checkbox" name=player[8] value="King Ralph">King Ralph
<BR><input type="checkbox" name=player[9] value="jim">jim
<BR><input type="checkbox" name=player[10] value="alec">alec
<BR><input type="checkbox" name=player[11] value="oil">oil
<BR><input type="checkbox" name=player[12] value="jimbob">jimbob
<BR><input type="checkbox" name=player[13] value="tinman">tinman
<BR><input type="checkbox" name=player[14] value="lion">lion
<BR><input type="checkbox" name=player[15] value="scarecrow">scarecrow
<BR><input type="checkbox" name=player[16] value="gremlins">gremlins
<BR><input type="checkbox" name=player[17] value="ALAN">ALAN
<BR><input type="checkbox" name=player[18] value="5 Iron">5 Iron
<BR><input type="checkbox" name=player[19] value="Scooby Doo">Scooby Doo
<BR><input type="checkbox" name=player[20] value="Fred Flinstone">Fred Flinstone
<BR><input type="checkbox" name=player[21] value="Barney Rubble">Barney Rubble
<BR><input type="checkbox" name=player[22] value="Kermit The Frog">Kermit The Frog
<BR><input type="checkbox" name=player[23] value="Miss Piggy">Miss Piggy
<BR>
<hr>
I then put this on the page it submits to

$player = $_POST["player"]; // get the array of selected values
$cnt = count($player); // count the number of selected items
for( $i = 0; $i < $cnt; $i++){
echo $player[$i], ' is a user!<br>'; // display each checked item
}

Problem is when I selected
Number 1, jim, alec and oil I get

level1 is a user!
is a user!
is a user!
is a user!
Seems as it is not getting passed the blank fields.
Any ideas.
Post Reply