0

Hi, I've been searching for hours for an answer to this.

I have a coupon saving website and I'm trying to allow people to print off a shopping list by checking checkboxes that once submitted will send you to a basic white page with just those item selected displayed so they can print the list that they created off. I've been able to do that with just a basic form and forwarding to a .php page with some basic PHP script.

So, if I had 10 checkboxes and only 2 of them were checked. Then, on the page they are sent to with the information they checked will have tons of white space in between. Like

1


5


It would be like that above. If the user checked 1 then didn't check 2,3,4 but did check 5, then the results page would display it with this big blank space. Which doesn't look good at all. Can anyone explain the code very simply to me that if a checkbox isn't checked that it won't return those blank spaces and will only send information to the next page if checked.

Thanks.

7
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by metalix
0

Depending on what your checkbox names are, you could do something like this:

Checkboxes:

<input type='checkbox' name='box1' value='item1' /><br />
<input type='checkbox' name='box2' value='item3' /><br />
<input type='checkbox' name='box3' value='item3' /><br />

PHP script:

<?php

// Check to see if first item is checked
if(isset($_POST['box1'])) {  
echo $_POST['box1'] . "<br />"; // Will print value(item1)
}

// Check to see if second item is checked
if(isset($_POST['box2'])) {  
echo $_POST['box2'] . "<br />"; // Will print value(item2)
}

// Check to see if third item is checked
if(isset($_POST['box3'])) {  
echo $_POST['box3'] . "<br />"; // Will print value(item3)
}

?>

Edited by Lsmjudoka: n/a

0

This works, of course, but it's not a very elegant solution and if you have many checkboxes on the page than (say 50) you will have to write that code 50 times, which I don't think you'll like :)
So, another easier solution is to set the names of the checkboxes to that they are sent to the next page as an array, then, on the next page, you just have to loop through the array:

<input type='checkbox' name='boxex[1]' value='item1' /><br />
<input type='checkbox' name='boxes[2]' value='item2' /><br />
.....
<input type='checkbox' name='boxes[9]' value='item9' /><br />
<input type='checkbox' name='boxes[10]' value='item10' />
<?php
if(isset($_POST['boxes']) && !empty($_POST['boxes'])) {
foreach($_POST['boxes'] as $k => $v) {
print $v . '<br />';
}
}
else {
print "Nothing checked";
}
?>

Edited by johny_d: n/a

0

I have another question. We type out a grocery list which is sometimes way more than 50 checkboxes. Right now, we have to type out the value=whatever text we write and then again outside of the code to make it appear in the form. What is an easy way so we don't have to type the same thing twice per checkbox in the code.

Like this

<input name="box1" type="checkbox" value="&lt;b&gt;Quaker True Delight Squares or Chewy Granola Bars $1.50&lt;/b&gt;&lt;br /&gt;
Use $0.75/1 from RP 2/14&lt;br /&gt;
&lt;b&gt;Final Price: $0.75 each!&lt;/b&gt;&lt;br /&gt;" /><b> Quaker True Delight Squares or Chewy Granola Bars $1.50</b>
&nbsp;&nbsp;&nbsp; &nbsp; Use $0.75/1 from RP 2/14
&nbsp;&nbsp; &nbsp;&nbsp; <b>Final Price: $0.75 each!</b>

Edited by frugalfreeloade: n/a

2

I didn't read your post, but I think I know your problem:

Name the checkboxes as "checkbox[]"

And then in the php processing use foreach ($_POST) to process the data.

0

If you google some info on using arrays it will greatly help in creating them and understanding them. e.g:

<?
$price[]=2.5;
$price[]=4;
$price[]=12.95;
print_r($price);

this will give Array ( [0] => 2.5 [1] => 4 [2] => 12.95 )

(it starts at 0)

hope this is helpful

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.