[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Pass Unchecked Boxes instead of Checked Boxes in PHP

I am trying to pass values for boxes that are unchecked instead of checked. My program is simple. Use a form to input a number. That number will display an image with a number, and include a check box to remove any checkboxes you dont want. The 3rd page should only display images/numbers that were not selected. See my code it will be much more clear. Currently though I can only see checkboxes that are checked. Appriciate the help.

Michael
PAGE 1:
<body>
 
<!--//began form -->
 
<form action="handle_form_1.php" method="post">
<!--ask for number oc columns to display for image -->
How many columns should the image display: <input type="text" name="column" size"4" />
 
<!--submit button-->
<input type="submit" name="submit" value="Submit" />
 
</body>
 
Page 2:
<?php
 
$column = trim($_POST['column']);
$images = array(
0 => '<img src="images/1.gif" />',
1 => '<img src="images/2.gif" />', 
2 => '<img src="images/3.gif" />',
3 => '<img src="images/4.gif" />',
4 => '<img src="images/5.gif" />', 
5 => '<img src="images/6.gif" />',
6 => '<img src="images/7.gif" />', 
7 => '<img src="images/8.gif" />', 
8 => '<img src="images/9.gif" />', 
9 => '<img src="images/10.gif" />',
10 => '<img src="images/11.gif" />',
11 => '<img src="images/12.gif" />', 
12 => '<img src="images/13.gif" />',
13 => '<img src="images/14.gif" />', 
14 => '<img src="images/15.gif" />', 
15 => '<img src="images/16.gif" />',
16 => '<img src="images/17.gif" />',
17 => '<img src="images/18.gif" />',
18 => '<img src="images/19.gif" />',
19 => '<img src="images/20.gif" />');
 
 
if($column == 0 || $column > 20)
{
		header("Location: askQ.html");
		
}else
{	
echo '<form action="handle_form_2.php" method="post" />';
echo '<table border="1">';	
 
	for($i = 0; $i < $column; $i++)
	{
		echo '<tr><td>';
		$var = $images[$i];
		print "$var";
		echo '<input type="hidden" name="unchecked['.htmlspecialchars($i).']" value="1">';
		echo 'Remove <input type="checkbox" name="checked['.htmlspecialchars($i).']" value="1">';
		echo '</td></tr>';
		
	}
		
echo '</table>';
echo '<input type="submit" name="submit" value="Submit" />';
}
 
?>
 
 
PAGE 3:
<?php
$images = array(
0 => '<img src="images/1.gif" />',
1 => '<img src="images/2.gif" />', 
2 => '<img src="images/3.gif" />',
3 => '<img src="images/4.gif" />',
4 => '<img src="images/5.gif" />', 
5 => '<img src="images/6.gif" />',
6 => '<img src="images/7.gif" />', 
7 => '<img src="images/8.gif" />', 
8 => '<img src="images/9.gif" />', 
9 => '<img src="images/10.gif" />',
10 => '<img src="images/11.gif" />',
11 => '<img src="images/12.gif" />', 
12 => '<img src="images/13.gif" />',
13 => '<img src="images/14.gif" />', 
14 => '<img src="images/15.gif" />', 
15 => '<img src="images/16.gif" />',
16 => '<img src="images/17.gif" />',
17 => '<img src="images/18.gif" />',
18 => '<img src="images/19.gif" />',
19 => '<img src="images/20.gif" />');
 
 
	foreach($_POST['checked'] as $key => $val)
	{
			$image = ($images[$key]);
			print "$image, ";
		
	}
	/*foreach($_POST['unchecked'] as $key)
	{
			$image = $images[$key];
			print "$image, ";
		
	}*/
 
?>

Open in new window

0
Proactive1
Asked:
Proactive1
  • 4
  • 3
1 Solution
 
Roger BaklundCommented:
You can not pass unchecked checkboxes. This is the nature of checkboxes, only checked checkboxes are sent to the server. However, you allready know which checkboxes was on the page, so it is easy to determine which was unchecked: all except those that was checked.
0
 
Proactive1Author Commented:
How though do I compare the arrays, and only print the unchecked items. I have a really hard time with arrays, and I know I need to compare them, but how, and what code to use I dont know.
0
 
Roger BaklundCommented:
There are many special functions in php to deal with arrays:

http://php.net/manual/en/book.array.php

The first thing that confuse beginners is the fact that a php array is a combination of a list and a dictionary type structure, called "associative array" in php lingo. A list is an easy construct to understand, an associative array is a list of key/value pairs. Some examples:
$list = array('green','red','blue');
 
echo count($list); # 3
 
echo $list[0] # 'green'
echo $list[1] # 'red'
echo $list[2] # 'blue'
 
# loop to echo all items in $list
foreach($list as $item)
  echo $item.'<br />';
 
$dict = array(
  'key1' => 'value 1', 
  'key2' => 'value 2',
  'key3' => 'value 3');
 
$list2 = array_keys($dict);   # array('item1','item2','item3')
$list3 = array_values($dict); # array('value 1','value 2','value 3');
 
echo $dict['key1']; # 'value 1'
 
# loop to echo all items in $dict
foreach($dict as $key=>$value)
  echo $key.'='.$value.'<br />';
 
# alternative method
foreach(array_keys($dict) as $key)
  echo $key.'='.$dict[$key];

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Proactive1Author Commented:
Thanks, that clears it up a little. I am going to play around with my code a little and see if I can figure out the solution with your example. Thanks for your help so far, Ill keep you posted.
0
 
Roger BaklundCommented:
Now back to your task. You have 20 images, and some of them are checked, some are not. Loop over all the items, if they are NOT checked, add them to the unchecked list:
$unchecked = array();
for($i=0;$i<20;$i++)  # loop over the 20 items
  if(!isset($_POST['checked'][$i])) # if item $i is NOT set...
    $unchecked[] = $i;              # ... add it to $unchecked

Open in new window

0
 
Proactive1Author Commented:
I got it to work, thanks to your little pointers, I under Stood what to do. Thanks. Here is my code now.
PAGE: 2
$column = trim($_POST['column']);
$images = array(
0 => '<img src="images/1.gif" />',
1 => '<img src="images/2.gif" />', 
2 => '<img src="images/3.gif" />',
3 => '<img src="images/4.gif" />',
4 => '<img src="images/5.gif" />', 
5 => '<img src="images/6.gif" />',
6 => '<img src="images/7.gif" />', 
7 => '<img src="images/8.gif" />', 
8 => '<img src="images/9.gif" />', 
9 => '<img src="images/10.gif" />',
10 => '<img src="images/11.gif" />',
11 => '<img src="images/12.gif" />', 
12 => '<img src="images/13.gif" />',
13 => '<img src="images/14.gif" />', 
14 => '<img src="images/15.gif" />', 
15 => '<img src="images/16.gif" />',
16 => '<img src="images/17.gif" />',
17 => '<img src="images/18.gif" />',
18 => '<img src="images/19.gif" />',
19 => '<img src="images/20.gif" />');
 
 
if($column == 0 || $column > 20)
{
		header("Location: askQ.html");
		
}else
{	
echo '<form action="handle_form_2.php" method="post" />';
echo '<table border="1">';	
 
	for($i = 0; $i < $column; $i++)
	{
		echo '<tr><td>';
		$var = $images[$i];
		print "$var";
		echo '<input type="hidden" name="unchecked[]" value='.htmlspecialchars($i).'>';
		echo 'Remove <input type="checkbox" name="checked[]" value='.htmlspecialchars($i).'>';
		echo '</td></tr>';
		
	}
		
echo '</table>';
echo '<input type="submit" name="submit" value="Submit" />';
}
 
?>
 
 
 
PAGE:3
<?php
$checked =  ($_POST['checked']);
$unchecked = ($_POST['unchecked']);
$images = array(
0 => '<img src="images/1.gif" />',
1 => '<img src="images/2.gif" />', 
2 => '<img src="images/3.gif" />',
3 => '<img src="images/4.gif" />',
4 => '<img src="images/5.gif" />', 
5 => '<img src="images/6.gif" />',
6 => '<img src="images/7.gif" />', 
7 => '<img src="images/8.gif" />', 
8 => '<img src="images/9.gif" />', 
9 => '<img src="images/10.gif" />',
10 => '<img src="images/11.gif" />',
11 => '<img src="images/12.gif" />', 
12 => '<img src="images/13.gif" />',
13 => '<img src="images/14.gif" />', 
14 => '<img src="images/15.gif" />', 
15 => '<img src="images/16.gif" />',
16 => '<img src="images/17.gif" />',
17 => '<img src="images/18.gif" />',
18 => '<img src="images/19.gif" />',
19 => '<img src="images/20.gif" />');
 
 
$diff = array_diff($unchecked,$checked);
 
foreach($diff as $key => $item)
{
	$image = $images[$key];
	print "$image, ";
}
?>

Open in new window

0
 
Roger BaklundCommented:
Some suggestions:

$column = (int) trim($_POST['column']);

This will force an integer value, at the same time preventing injection attacks.

You could populate your $images array like this:

$images = array();
for($i = 0; $i < 20; $i++)
  $images[] = '<img src="images/'.($i+1).'.gif" alt="" />';

(The img html element should have an alt attribute, according to html specs)

There is no need to use htmlspecialchars() on the $i variable, because it is an integer. htmlspecialchars() escapes some special html characters from strings: ", ', &, < and >

The tag ending /> is for singleton elements only (img and input) , don't use it on elements with content (form).

You forgot to end the form with </form>

Instead of

        $image = $images[$key];
        print "$image, ";

...you could just do

       echo $images[$key].', ';

echo/print has the same effect, you should stick to either one of them, not combine. It's not an error, it's just cleaner... :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now