selecting checkboxes
Moderator: General Moderators
selecting checkboxes
i have a form i want to display for a user to update choices, right now i'm going to ignore arrays they don't recheck, but since they arrays display as checkboxes that have varying amounts on each row, and some rows wont be complete (as in i only have 2 things on the rown that has 5 columns), i'm trying to think of a way to populate these as selected when making the page.
the values are held in a sql db
any ideas or advice greatly appreciated
the values are held in a sql db
any ideas or advice greatly appreciated
$_POST['variable']['foo']
A good debug-method when using arrays is using print_r().
A good debug-method when using arrays is using print_r().
Code: Select all
<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>Of course, but at least you would understand more of your problem.m3rajk wrote:if i have the array setting right i wont need debugging with php... all errors will be in the db.
Debugging also means to figure out a way to get something right, so if you have problems with something, try analyzing it with various approaches (one i mentioned).
do you have any suggestions for the other issue?
btw, if it helps, this is, with some edits, the function that displays the control panel that i'm trying to have the select boxes show up pre-selectedi was hoping ot get an idea of how to populate the check boxes without doing each one individually (maybe by group (sports, etc) )and def without going in and out of php
btw, if it helps, this is, with some edits, the function that displays the control panel that i'm trying to have the select boxes show up pre-selected
Code: Select all
function interest(){ # in interests
include(""); # variable inclusion
$result; $selections;
$un=$_COOKIE['un']; $pw=$_COOKIE['pw']; # variables to talk to the db
$db=mysql_pconnect(host, user, pw) or die("cannot access db"); # connect to the db
$fyd=mysql_select_db(dbname, $db); # get the db
$memfind=mysql_query("SELECT * FROM table WHERE username=$un", $db);
if(mysql_num_rows($memfind)==0){ # problem
echo '<h1>We cannot find you. Are you sure you <a href="join.php">Joined?</a>';
}else{ # we found the user
$maininf=mysql_fetch_array($memfind);
$pwc=$maininf['password'];
if($pw!=$pwc){ # cannot verify user
echo "<p>Cannot verify that you are $un. Password error (submitted pw does not match file)</p></center></body></html>";
}else{ # we know who we're dealin with
$uin=$maininf['uid']; $intToUp=''; $selections='';
if(isset($_POST['act'])) { # update the db
# get the submitted information about the interests
$smusic=$_POST['music']; $sbooks=$_POST['books']; $smovies=$_POST['movies'];
$ssports=$_POST['sports']; $smisc=$_POST['misc']; $spets=$_POST['pets'];
# turn the submitted interests (sinterests) array into strings
$movies=join(' ',$smovies); $books=join(' ',$sbooks); $misc=join(' ',$smisc);
$sports=join(' ',$ssports); $music=join(' ',$smusic); $pets=join(' ',$spets);
# clean the lists incase somenoe did do something to screw with the input
$movies=clean($movies); $books=clean($books); $misc=clean($misc); $sports=clean($sports);
$music=clean($music); $pets=clean($pets);
if($movies!=''){ $intToUp=$intToUp."movies=$movies"; } # get the update list
if($books!=''){ if($intToUp!=''){ $intToUp=$intToUp.', '; } $intToUp=$intToUp."books=$books"; }
if($music!=''){ if($intToUp!=''){ $intToUp=$intToUp.', '; } $intToUp=$intToUp."music=$music"; }
if($pets!=''){ if($intToUp!=''){ $intToUp=$intToUp.', '; } $intToUp=$intToUp."pets=$pets"; }
if($sports!=''){ if($intToUp!=''){ $intToUp=$intToUp.', '; } $intToUp=$intToUp."sports=$sports"; }
if($misc!=''){ if($intToUp!=''){ $intToUp=$intToUp.', '; } $intToUp=$intToUp."misc=$misc"; }
$update=mysql_query("UPDATE table SET $intToUp WHERE uid=$uin", $db);
if(mysql_affected_rows($db)==1){ $result='Update Successful'; }
else{ $result='Error on update. Please try again.'; }
} # done with the possible update, retrieve info and display
$interestfind=mysql_query("SELECT * FROM table WHERE uid=$uin", $db);
if(mysql_num_rows($interestfind)==0){ # error
exit("<h1>Database failure. Please alert the administration. While your Main Info is on Record, your Interest information has disappeared</h1>");
}else{ # we got the info
$interests=mysql_fetch_array($interestfind);
$selections='<br />Movies:'.$interests['movies'].'<br />Books:'.$interests['books'].'<br />Music:'.$interests['music'].'<br />Pets:'.$interests['pets'].'<br />Sports:'.$interests['sports'].'<br />Misc:'..$interests['misc'];
}
# make the page
echo <<<END
<p>$result</p>
<p>Your Current Selections (updating only affects changed groups. only the new selections will be in the update): $selections</p>
<form action="$_SERVER[PHP_SELF]" method="POST">
<input type="hidden" name="fn" value="interest"><input type="hidden" name="act" value="process">
<table frame="void" bgcolor="#000000" text="#c8c8c8" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" align="center"><!-- inner table for interest: music -->
<table frame="box" bgcolor="#000000" border="1" cellpadding="0" cellspacing="0" text="#c8c8c8" width="100%">
<tr><th colspan="7">Please check off all Genres of Music that interest you</th></tr>
<tr><td><input type="checkbox" name="music[]" value="Blues"> Blues</td><td><input type="checkbox" name="music[]" value="Christian"> Christian</td><td><input type="checkbox" name="music[]" value="classical"> Classical</td><td><input type="checkbox" name="music[]" value="Country-Western"> Country/Western</td><td><input type="checkbox" name="music[]" value="Dance"> Dance</td><td><input type="checkbox" name="music[]" value="Folk"> Folk</td><td><input type="checkbox" name="music[]" value="Gospell"> Gospel</td></tr>
<tr><td><input type="checkbox" name="music[]" value="Jazz"> Jazz</td><td><input type="checkbox" name="music[]" value="Opera"> Opera</td><td><input type="checkbox" name="music[]" value="Other"> Other</td><td><input type="checkbox" name="music[]" value="Polka"> Polka</td><td><input type="checkbox" name="music[]" value="Pop"> Pop</td><td><input type="checkbox" name="music[]" value="Rap"> Rap</td><td><input type="checkbox" name="music[]" value="Rock"> Rock</td></tr>
<tr><td><input type="checkbox" name="music[]" value="Salsa"> Salsa</td><td><input type="checkbox" name="music[]" value="Techno"> Techno</td><td> </td><td> </td><td> </td><td> </td><td> </td></tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" align="center"><!-- inner table for interest: Movies -->
<table frame="box" bgcolor="#000000" border="1" cellpadding="0" cellspacing="0" text="#c8c8c8" width="100%">
<tr><th colspan="6">Please check off all Genres of Movies that interest you</th></tr>
<tr><td><input type="checkbox" name="movies[]" value="Action"> Action</td><td><input type="checkbox" name="movies[]" value="Animated"> Animated</td><td><input type="checkbox" name="movies[]" value="Comedy"> Comedy</td><td><input type="checkbox" name="movies[]" value="Childrens"> Children's (Disney)<!-- ' (to fool xemacs) --></td><td><input type="checkbox" name="movies[]" value="Documentary"> Documentary</td><td><input type="checkbox" name="movies[]" value="Drama"> Drama</td></tr>
<tr><td><input type="checkbox" name="movies[]" value="Fantasy"> Fantasy</td><td><input type="checkbox" name="movies[]" value="Foreign"> Foreign</td><td><input type="checkbox" name="movies[]" value="Horror"> Horror</td><td><input type="checkbox" name="movies[]" value="Other"> Other</td><td><input type="checkbox" name="movies[]" value="Romance"> Romance</td><td><input type="checkbox" name="movies[]" value="Science-Fiction"> Science-Fiction</td></tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" align="center"><!-- inner table for interest: Books -->
<table frame="box" bgcolor="#000000" border="1" cellpadding="0" cellspacing="0" text="#c8c8c8" width="100%">
<tr><th colspan="7" >Please check off all Genres of Books that interest you</th></tr>
<tr><td><input type="checkbox" name="books[]" value="Comedy"> Comedy</td><td><input type="checkbox" name="books[]" value="Comic"> Comic</td><td><input type="checkbox" name="books[]" value="Fantasy"> Fantasy</td><td><input type="checkbox" name="books[]" value="Other"> Other</td><td><input type="checkbox" name="books[]" value="Horror"> Horror</td><td><input type="checkbox" name="books[]" value="Mystery"> Mystery</td><td><input type="checkbox" name="books[]" value="Romance"> Romance</td></tr>
<tr><td><input type="checkbox" name="books[]" value="Science-Fiction"> Science-Fiction</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td></tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" align="center"> <!-- inner table for interest: Sports-->
<table frame="box" bgcolor="#000000" border="1" cellpadding="0" cellspacing="0" text="#c8c8c8" width="100%">
<tr><th colspan="7">Please check off all Sports that interest you</th></tr>
<tr><td><input type="checkbox" name="sports[]" value="Baseball"> Baseball</td><td><input type="checkbox" name="sports[]" value="Basketball"> Basketball</td><td><input type="checkbox" name="sports[]" value="Curling"> Curling</td><td><input type="checkbox" name="sports[]" value="Dance"> Dance</td><td><input type="checkbox" name="sports[]" value="Fencing"> Fencing</td></tr>
<tr><td><input type="checkbox" name="sports[]" value="Field-Hockey"> Field Hockey</td><td><input type="checkbox" name="sports[]" value="Fishing"> Fishing</td><td><input type="checkbox" name="sports[]" value="Football"> Football</td><td><input type="checkbox" name="sports[]" value="Gymnastics"> Gymnastics</td><td><input type="checkbox" name="sports[]" value="Hockey"> Hockey</td></tr>
<tr><td><input type="checkbox" name="sports[]" value="Ice-Skating"> Ice Skating</td><td><input type="checkbox" name="sports[]" value="Inline-Skating"> Inline Skating</td><td><input type="checkbox" name="sports[]" value="Luge"> Luge</td><td><input type="checkbox" name="sports[]" value="Other"> Other</td><td><input type="checkbox" name="sports[]" value="Skate-Boarding"> Skate Boarding</td></tr>
<tr><td><input type="checkbox" name="sports[]" value="Sky-Diving"> Sky Diving</td><td><input type="checkbox" name="sports[]" value="Snow-Boarding"> Snow Boarding</td><td><input type="checkbox" name="sports[]" value="Street-Luge"> Street Luge</td><td><input type="checkbox" name="sports[]" value="Surfing"> Surfing</td><td><input type="checkbox" name="sports[]" value="Swimming"> Swimming</td></tr>
<tr><td><input type="checkbox" name="sports[]" value="Track-Field"> Track & Field</td><td><input type="checkbox" name="sports[]" value="Tennis"> Tennis</td><td><input type="checkbox" name="sports[]" value="Volleyball"> Volleyball</td><td> </td><td> </td></tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" align="center"> <!-- inner table for interest: Pets-->
<table frame="box" bgcolor="#000000" border="1" cellpadding="0" cellspacing="0" text="#c8c8c8" width="100%">
<tr><th colspan="7">Please check off all Pets that interest you</th></tr>
<tr><td><input type="checkbox" name="pets[]" value="Birds"> Birds</td><td><input type="checkbox" name="pets[]" value="Cats"> Cats</td><td><input type="checkbox" name="pets[]" value="Dogs"> Dogs</td><td><input type="checkbox" name="pets[]" value="Fish"> Fish</td><td><input type="checkbox" name="pets[]" value="Gerbils"> Gerbils</td><td><input type="checkbox" name="pets[]" value="Hamsters"> Hamsters</td><td><input type="checkbox" name="pets[]" value="Hermit-Crabs"> Hermit Crabs</td></tr>
<tr><td><input type="checkbox" name="pets[]" value="Horses"> Horses</td><td><input type="checkbox" name="pets[]" value="Lizards"> Lizards</td><td><input type="checkbox" name="pets[]" value="Mice"> Mice</td><td><input type="checkbox" name="pets[]" value="Other"> Other</td><td><input type="checkbox" name="pets[]" value="Rabbits"> Rabbits</td><td><input type="checkbox" name="pets[]" value="Rats"> Rats</td><td><input type="checkbox" name="pets[]" value="Snakes"> Snakes</td></tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" align="center"> <!-- inner table for interest: Misc-->
<table frame="box" bgcolor="#000000" border="1" cellpadding="0" cellspacing="0" text="#c8c8c8" width="100%">
<tr><th colspan="4">Please check off all Miscellaneous Interests that interest you</th></tr>
<tr><td><input type="checkbox" name="misc[]" value="Aircraft-C"> Aircraft (Commercial)</td><td><input type="checkbox" name="misc[]" value="Aircraft-M"> Aircraft (Military)</td><td><input type="checkbox" name="misc[]" value="Cars-Trucks"> Cars/Trucks</td><td><input type="checkbox" name="misc[]" value="Computers-G"> Computer Games</td></tr>
<tr><td><input type="checkbox" name="misc[]" value="Computers-H"> Computer Hardware</td><td><input type="checkbox" name="misc[]" value="Computer-P"> Computer Programming</td><td><input type="checkbox" name="misc[]" value="Occult"> Occult</td><td><input type="checkbox" name="misc[]" value="Other"> Other</td></tr>
<tr><td><input type="checkbox" name="misc[]" value="Religions"> Religions</td><td><input type="checkbox" name="misc[]" value="RPG"> Role Playing Games (RPGs)</td><td> </td><td> </td></tr>
</table>
</td>
</tr>
<tr>
<td align="center"><input type="submit" value="Update Interests"></td>
<td align="center"><input type="reset" value="Nah, Nothing's Changed"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
END;
}
}
}Something similiar to this?
Code: Select all
<?php
// assign arrays
// a)
$interests['sports'] = array('Dart','Soccer','Hocket','KittyThrow');
// or b)
$interests['sports'][] = 'Dart';
$interests['sports'][] = 'Soccer';
$interests['sports'][] = 'Hocket';
$interests['sports'][] = 'KittyThrow';
// or c) using database and "select distinct sports from table" query
// echo start of table/form here...
foreach ($interests as $val) {
echo '
<tr><td><input type="checkbox" name="movies[]" value="'.$val.'"></td></tr>';
}
// echo end of table/form here...
}
?>i don't want to go in and out of php. i want to use the heredoc.
and the number of interests exceeds the number of choices in a row, and the number varies based on the catagory.
for select boxes in the join, i use a function that itereates through the selection and if your previous submission is the same as one of them, it give that option to the string of them as <option value="whatever" selected>whatever</option> instead of appending <option value="whatever">whatever</option> , but that's used for error checking. (and the function is reused in the control panels where i use seelct boxes)
this is similar to that, i want to be able to have the html pre-select the checkboxes correspoding to values they selected previously (returned from tthe database)
incase it wasn't clear, let's say i have 15 choices but the user only chose 5 of them last time. how do i make JUST THOSE 5 selected?
and the number of interests exceeds the number of choices in a row, and the number varies based on the catagory.
for select boxes in the join, i use a function that itereates through the selection and if your previous submission is the same as one of them, it give that option to the string of them as <option value="whatever" selected>whatever</option> instead of appending <option value="whatever">whatever</option> , but that's used for error checking. (and the function is reused in the control panels where i use seelct boxes)
this is similar to that, i want to be able to have the html pre-select the checkboxes correspoding to values they selected previously (returned from tthe database)
incase it wasn't clear, let's say i have 15 choices but the user only chose 5 of them last time. how do i make JUST THOSE 5 selected?