...

View Full Version : Passing Info From Html Checkbox to php



walker6o9
12-05-2008, 08:32 PM
I'm trying to create a bunch of checkboxes that pass information to a php script in code.

When "Page1" is checked, I need a php variable $page1 to be set equal to false. The same for Page2 and Page3. When the button is clicked, a php variable $run becomes set equal to true.



<form name="myform" action="" method="POST">
<div align="center"><br>
<input type="checkbox" name="option1" value="Page1"> Page1<br>
<input type="checkbox" name="option2" value="Page2" checked> Page2<br>
<input type="checkbox" name="option3" value="Page3"> Page3<br>
<br>
</div>
</form>
<button type="button">Click Here</button>

Fumigator
12-05-2008, 09:17 PM
As you've noticed, checkboxes that are unchecked do not show up at all on the back end. This is a "feature" of the HTTP request and is not unique to PHP. ASP, Cold Fusion, and all other HTTP parsers have to deal with it.

There is a way to combat it though. Create input elements, type="hidden", using the same name as the checkbox. This hidden element must be placed before the checkbox. Set the value of this hidden element to "false", or "0", or "off", or whatever.

Set the value of the checkbox itself to "true", "1", or "on".

With this hidden element, the HTTP request now brings its value across to PHP, unless the checkbox is checked, in which case the checkbox's value is brought across to PHP.



<form name="myform" action="" method="POST">
<div align="center"><br>
<input type="hidden" name="option1" value="false">
<input type="hidden" name="option2" value="false">
<input type="hidden" name="option3" value="false">
<input type="checkbox" name="option1" value="Page1"> Page1<br>
<input type="checkbox" name="option2" value="Page2" checked> Page2<br>
<input type="checkbox" name="option3" value="Page3"> Page3<br>
<br>
</div>
</form>
<button type="button">Click Here</button>

mlseim
12-05-2008, 09:24 PM
There's more than one way to do this, but since you're using variables
instead of an array (example, you're using $page1, $page2, $page3) ...

The form:
<form name="myform" action="test.php" method="POST">
<div align="center"><br>
<input type="checkbox" name="option[]" value="1"> Page1<br>
<input type="checkbox" name="option[]" value="2" checked> Page2<br>
<input type="checkbox" name="option[]" value="3"> Page3<br>
<br>
<input type="submit" name="submit" value="Click Here">
</div>
</form>

The PHP script:


<?php

$page1=0;
$page2=0;
$page3=0;

if($_POST['option']){
$opts = $_POST['option'];
foreach ($opts as $o) {
if($o == 1){$page1=1;}
if($o == 2){$page2=1;}
if($o == 3){$page3=1;}
}
}
echo $page1.$page2.$page3

?>

walker6o9
12-05-2008, 09:25 PM
So, I tried re-working your code, to set a variable "run" that is normally equal to zero, but gets set equal to one when the button is clicked. The problem is that run is always set to one.




<form name="myform" action="starter_pdfmerge.php" method="POST">
<div align="center"><br>
<input type="checkbox" name="option[]" value="1"> Page1<br>
<input type="checkbox" name="option[]" value="2" checked> Page2<br>
<input type="checkbox" name="option[]" value="3"> Page3<br>
<br>
<input type="submit" name="option[]" value="Click Here"> Click Here <br>
</div>
</form>


<?php


$page1=0;
$page2=0;
$page3=0;
$run = 0;

if($_POST['option']){
$opts = $_POST['option'];
foreach ($opts as $o) {
if($o == 1){$page1=1;}
if($o == 2){$page2=1;}
if($o == 3){$page3=1;}
if($o == "Click Here"){$run=1;}
}
}
echo $page1.$page2.$page3.$run;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum