Page 1 of 1
Many Cheboxes
Posted: Tue Jul 23, 2002 12:28 pm
by pentium3id
Hi all,
this is my first post here, cause I have a problem,
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
Handling large lists
Posted: Tue Jul 23, 2002 12:51 pm
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.
Excellent!
Posted: Tue Jul 23, 2002 1:16 pm
by dlgilbert
Good tip! I've always wondered how to do that in an "un-kludgy" way.

Posted: Sun Nov 03, 2002 8:21 pm
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.
Posted: Sun Nov 03, 2002 9:12 pm
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
<html>
<head>
<title>Who are the users</title>
</head>
<body>
<?php
if( isset($_POSTї'submit'] ) ){ // if the form has been submitted
?>
<h4>The users are:</h4>
<?php
if( isset($_POSTї"users"]) ){ // check to see if any checkboxes selected
$users = $_POSTї"users"]; // get the array of selected values
$cnt = count($users); // count the number of selected items
for( $i = 0; $i < $cnt; $i++){
echo $usersї$i], ' is a user!<br>'; // display each checked item
}
}else{
echo "There are no users!"; // no checkboxes checked
}
}else{ // the form has not been submitted yet, so display it
?>
<h4>Who are the users?</h4>
<form action="testcheckboxes.php" method="post">
<input type="checkbox" name="usersї]" value="George">George<br>
<input type="checkbox" name="usersї]" value="Tony">Tony<br>
<input type="checkbox" name="usersї]" value="Saddam">Saddam<br>
<input type="checkbox" name="usersї]" value="Vladimir">Vladimir<br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
?>
</body>
</html>
hope this helps,
Sam
Posted: Tue Nov 05, 2002 11:12 am
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
Posted: Wed Nov 06, 2002 10:50 am
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.