Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Checkboxes! IFs, POSTs and arrays...

Featured Replies

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

Hi Steve,

 

On the second page I would loop through the database results again and check to see which ones are not in the post array.

 

That will then give you all of the un-ticked one.

 

The code would be somethign similar to this

<?php

// Set a blank array to hold all of the unticked items
$unticked = array();

// Assign the post values to a variable
$ticked = $_POST;

// Loop through the database results
while($row = mysql_fetch_array($result)) {

// Check if the current item is not in the ticked array
if(!in_array($row['checklistitem'], $ticked)) {

	// The item does not appear in the ticked array so the user must have left it unticked
	// Add it to the unticked array
	$unticked[] = $row['checklistitem'];

}

}

// Check if there are any unticked items and display them if so
if(count($unticked) > 0) {

// Display checkboxes

}

?>

Edited by vertmonkee

  • Author

Thanks so much for replying vertmonkee! I just tried to run this script and added some basic echos to see if it's displaying the right output.

 

My problems are:

 

• Checking boxes on the first page doesn't seem to effect the code below. It always states 'There are boxes unchecked' even if all the checkboxes are ticked on the previous page?

 

• I don't know how to show which check boxes haven't been checked?

 

Please help if you can! Thankyou!!

 

 

<?php
// Set a blank array to hold all of the unticked items
$unticked = array();
// Assign the post values to a variable
$ticked = $_POST;
// Loop through the database results
while($row = mysql_fetch_array($result)) {
       // Check if the current item is not in the ticked array
       if(!in_array($row['checklistitem'], $ticked)) {
               // The item does not appear in the ticked array so the user must have left it unticked
               // Add it to the unticked array
               $unticked[] = $row['checklistitem'];
       }
}
// Check if there are any unticked items and display them if so
if(count($unticked) > 0) {
       echo "There are boxes unchecked!";
}
else {
echo "You've checked all the boxes!";
}
?>

I just put together a quick script to try and test this, let me know if you would like to see it. I just used a coded array rather than pulling it from a database.

 

I think the problem occurs where we check if the value is in the array. All the values of the array are yes. What actuall needs to happen is to check the array key.

 

I have just commented out the old if statement and put the new one in place.

 

Let me know if this works.

 

<?php
// Set a blank array to hold all of the unticked items
$unticked = array();
// Assign the post values to a variable
$ticked = $_POST;
// Loop through the database results
while($row = mysql_fetch_array($result)) {
       // Check if the current item is not in the ticked array
       //if(!in_array($row['checklistitem'], $ticked)) {
       if(!array_key_exists($row['checklistitem'], $ticked)) {
               // The item does not appear in the ticked array so the user must have left it unticked
               // Add it to the unticked array
               $unticked[] = $row['checklistitem'];
       }
}
// Check if there are any unticked items and display them if so
if(count($unticked) > 0) {
       echo "There are boxes unchecked!";
}
else {
echo "You've checked all the boxes!";
}
?>

Edited by vertmonkee

  • Author

Hmm.. Still getting the same thing :(

 

How annoying! (not you my script! :) ) I was considering using the 'array_diff' but have yet to get that working..

 

Cheers for your help dude!

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.