jamkelvl Posted January 16, 2010 Share Posted January 16, 2010 Okay so I have this form that connects to my database and grabs all data from a table called employees. The employees are then all displayed on the page with check boxes. Basically, the form adds specific employees to another table in the database under a field called "Assigned To". Anyways, all of this works fine, the data is stored with an array and blah blah blah. Lets say a user tries to fill out the form and selects 3 employees, clicks submit but forgot to fill out a required field. The form will then highlight all missing fields and posts all fields that have already been entered (nothing as gone into the database yet). I want my form to remember what checkboxes have been selected. How can I do this? What I have thus far, <?php //all other code omitted // Attempt database connection $connect = mysql_connect($hostname, $username, $password); if($connect == false){ echo('<p class="error">We are having technical difficulties and apologize for the inconvenience. Please try again later.</p>'); } // Select database $db = mysql_select_db($dbid); // query database $select = "SELECT * FROM employees ORDER BY name"; $result = mysql_query($select); // get results while($row = mysql_fetch_array($result)){ extract($row); echo ''.$name.'<input style="margin-right: 75%; float: right;" type="checkbox" name="assignedTo[]" id="assignedTo" value="'.$initials.'" /><br/><br/>'; } mysql_close($connection); ?> Link to comment https://forums.phpfreaks.com/topic/188709-arrays-and-checkboxes/ Share on other sites More sharing options...
jamkelvl Posted January 16, 2010 Author Share Posted January 16, 2010 Update code: <?php // get results while($row = mysql_fetch_array($result)){ extract($row); echo ''.$name.'<input style="margin-right: 75%; float: right;" type="checkbox" name="assignedTo[]" id="assignedTo" value="'.$initials.'" /><br/><br/>'; foreach ($_POST['assignedTo'] as $value) { if ($value == $initials) { echo ''.$name.'<input style="margin-right: 75%; float: right;" type="checkbox" name="assignedTo[]" id="assignedTo" value="'.$initials.'" checked /><br/><br/>'; } } } ?> Now, when the form is re-submitted it remembers what was checked but displays checked employees twice. Link to comment https://forums.phpfreaks.com/topic/188709-arrays-and-checkboxes/#findComment-996195 Share on other sites More sharing options...
jamkelvl Posted January 16, 2010 Author Share Posted January 16, 2010 Final result, lol thanks for letting me post Sometimes all you need is to write out what you're trying to do and BAM! <?php // get results while($row = mysql_fetch_array($result)){ extract($row); if (in_array($initials, $_POST['assignedTo'])) { // do nothing } else { echo ''.$name.'<input style="margin-right: 75%; float: right;" type="checkbox" name="assignedTo[]" id="assignedTo" value="'.$initials.'" /><br/><br/>'; } foreach ($_POST['assignedTo'] as $value) { if ($value == $initials) { echo ''.$name.'<input style="margin-right: 75%; float: right;" type="checkbox" name="assignedTo[]" id="assignedTo" value="'.$initials.'" checked /><br/><br/>'; } } } ?> Link to comment https://forums.phpfreaks.com/topic/188709-arrays-and-checkboxes/#findComment-996203 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.