nomis Posted August 8, 2009 Share Posted August 8, 2009 Hi, I'm sorry if this has been answered elsewhere, but I'm having some trouble keeping some checkboxes that were called as an array to stay sticky if the form errs on other required fields. Here's what I have have: <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table width=500 border=0 cellpadding=5> <tr> <td valign="top"><label for "person_type_code">Person Type:</label> </td> <td><?php $r = mysqli_query($dbc, 'select * from person_type order by person_type'); while ($row = mysqli_fetch_array($r)) { echo '<input type="checkbox" id="person_type" name="person_type_code[]" ' ; echo 'value="' . $row[0] .'"'; if (isset($_POST['person_type_code'])) { echo ' checked="checked"'; } echo '> ' . $row[1] . '<br />'; } ?> </td> This is unfortunately making EVERY checkbox show as checked, not just the ones that the user has selected. I need this form to come back with only the fields that a person has selected (one or more... a person may have many types) to appear checked. Can someone see what is wrong with my code? I have tried using $row in place of $_POST['person_type_code'] but it has the same effect. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/169296-solved-sticky-checkboxes/ Share on other sites More sharing options...
wildteen88 Posted August 8, 2009 Share Posted August 8, 2009 Change this if (isset($_POST['person_type_code'])) to if (isset($_POST['person_type_code']) && in_array($row[0], $_POST['person_type_code'])) Quote Link to comment https://forums.phpfreaks.com/topic/169296-solved-sticky-checkboxes/#findComment-893378 Share on other sites More sharing options...
nomis Posted August 8, 2009 Author Share Posted August 8, 2009 Got it... thanks! (of course it had to do with it being in an array!) much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/169296-solved-sticky-checkboxes/#findComment-893381 Share on other sites More sharing options...
TheWildJacko Posted June 19, 2012 Share Posted June 19, 2012 Thank you!! I had a similar problem and for the life of me I could not figure it out until I came here. Quote Link to comment https://forums.phpfreaks.com/topic/169296-solved-sticky-checkboxes/#findComment-1355303 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.