stevejcoates Posted January 11, 2011 Share Posted January 11, 2011 Hi, I have a set of checkboxes a bit like a checklist and I then want the items that aren't checked to displayed. On the first page, I have this code: <? while($row = mysql_fetch_array($result)) { ?> <form action="packed.php" method="post"> <div> <label><input type="checkbox" name="<? echo stripslashes($row['checklistitem']); ?>" value="yes"> <? echo stripslashes($row['checklistitem']); ?> </label> </div> This basically takes an array from a database. The info is then posted to the next page which hopefully will display the boxes which are 'unticked' or unchecked if you will. The code I have on the second page is: <?php $query_string = ""; if ($_POST) { $kv = array(); foreach ($_POST as $key => $value) { $kv[] = "$key"; } $query_string = join("<br />", $kv); } else { $query_string = $_SERVER['QUERY_STRING']; } echo $query_string; ?> At the moment, it returns the array items that were checked but I want the unchecked ones to be shown. Is that possible? Thanks very much! -Steve Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 11, 2011 Share Posted January 11, 2011 Checkbox values are only sent in the $_POST array if they are checked, so in order to display the unchecked ones, you'd need to compare the ones that are checked to the list of all that were available and list the ones that are not in both sets, probably using array_diff. It will however be much easier to have each group of checkboxes set as an array of form elements, using the same name= attribute, like name="group1[]", and make each value= attribute unique. Here's a quick example you can paste in and run to give you a starting point. <?php $chbx = array( 10, 20, 30, 40, 50, 60 ); if( isset($_POST['submitted']) && $_POST['submitted'] == 'yes' ) { print_r(array_diff( $chbx, $_POST['chbx'] )); } echo '<form action="" method="post">'; foreach( $chbx as $k => $v ) { echo "$v <input type=\"checkbox\" name=\"chbx[$k]\" value=\"$v\"><br>\n"; } echo '<input type="hidden" name="submitted" value="yes">'; echo '<input type="submit" value="Go">'; echo '</form>'; ?> Quote Link to comment Share on other sites More sharing options...
stevejcoates Posted January 12, 2011 Author Share Posted January 12, 2011 Thanks for the reply Pikachu2000! Your method worked perfectly using the code you provided however, I can't seem to be able to pass a MySQL array into it instead of a provided array so as you have shown me? I'm currently writing: <?php $chbx = mysql_query ("SELECT * FROM checklist"); while ($row = mysql_fetch_array($chbx, MYSQL_NUM)) { printf("%s <br />", $row[0], $row[1]); } ?> This gives me a list of the items on the database but I can't see a way of putting these into an 'input'. Can you help at all? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 12, 2011 Share Posted January 12, 2011 Can you post some sample data, and the names of the database fields it comes from? Quote Link to comment Share on other sites More sharing options...
stevejcoates Posted January 12, 2011 Author Share Posted January 12, 2011 Yea sure! The database table is called 'checklist'. There's only one field which is 'checklistitem'. The data that is comes from is items such as 'jacket, gloves, socks, trousers". It's sort of a packing checklist. At the top of my page I have: <? session_start(); require("connection.php"); $result = mysql_query("SELECT * FROM checklist"); ?> If you like to see it in action, it's at this address: http://www.stevecoat.es/check1 Thanks so much for your help! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 12, 2011 Share Posted January 12, 2011 If that's the only field, all you have to do is build your array from the query. Here's an example, storing the array in a $_SESSION var so the comparison can be done in the next script. Then it's just a matter of substituting the values into the code chunk above to do the comparison. Let me know if anything doesn't make sense. $query = "SELECT `checklistitem` FROM `checklist`"; if( $result = mysql_query($query) ) { if( mysql_num_rows($result) > 0 ) { $i = 1; $_SESSION['compare'] = array(); while( $array = mysql_fetch_row($result) ) { echo ucwords($array[0]) . ": <input type=\"checkbox\" name=\"chbx[$i]\" value=\"{$array[0]}\"><br>\n"; $_SESSION['compare'][$i] = $array[0]; } } } Quote Link to comment 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.