Jump to content

Display unchecked 'checkboxes' - Arrays, IFs and POSTs!


stevejcoates

Recommended Posts

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

 

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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];
	}
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.