Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   Check Boxes in PHP (https://www.warriorforum.com/programming/238644-check-boxes-php.html)

stevenc777 21st July 2010 10:16 AM

Check Boxes in PHP
 
Can anyone tell me how to use check boxes in the following php files?

contact form - contact.php
template for mail - contact-mail-html.tpl.php
contact-process.php - validates form

heroic 21st July 2010 11:23 AM

Re: Check Boxes in PHP
 
Since you have not provided the code for these files, i'll let u know the basic behind checkboxes in PHP

say ur html goes as <input type="checkbox" name="my_checkBox" value="myValue" />

then in PHP
<?=(isset($_PHP['my_checkBox']))?'checkbox was checked':'It was not checked'; ?>

luminousnerd 21st July 2010 06:03 PM

Re: Check Boxes in PHP
 
It depends what you want to do? PHP doesn't actually output check boxes, that's done with HTML. PHP is for using the data it gets from that checkbox, either to change what the user sees, put the info in a database, or something like that.

A checkbox is as simple as

<input type="checkbox">

Though you'll probably want a name and a value as well to do anything useful with it.

godwinsam 21st July 2010 10:29 PM

Re: Check Boxes in PHP
 
Hi,

Below I am mentioned the code for check boxes in php with form action. Please find it.

<?php
$box=$_POST['box'];

while (list ($key,$val) = @each ($box)) {
echo "$val,";
}

echo "<form method=post action=''>";
echo "<table border='0' cellspacing='0' style='border-collapse: collapse' width='100' >
<tr bgcolor='#ffffff'>
<td width='25%'><input type=checkbox name=box[] value='John'></td>
<td width='25%'>John</td>
<td width='25%'><input type=checkbox name=box[] value='Mike'></td>
<td width='25%'>Mike</td>
<td width='25%'><input type=checkbox name=box[] value='Rone'></td>
<td width='25%'>Rone</td>
</tr>
<tr><td colspan =6 align=center><input type=submit value=Select></form></td></tr>
</table>";
?>

Itamar 24th July 2010 11:41 PM

Re: Check Boxes in PHP
 
you need to have the checkbox added in the main file (the form)

and then in PHP the only thing you really need to use is
the isset() command , since a checkbox in PHP will never be available in the $_POST array
unless it's checked.


If it's checked , it will appear as TRUE , or 1. so the only real thing left to do when working it out on the php side
is to check if it's SET with isset($_POST["mycheckbox"])

WarriorForSuccess 24th August 2010 11:35 PM

Re: Check Boxes in PHP
 
It won't equal TRUE or 1... It'll equal the value. i.e. John, Mike, Rone

isset is appropriate; just make sure you know what you're getting as the value. This is a checkbox array. It's not as straight-forward. You'll get an array called "box" if ANY of the checkboxes are set, and then you have to iterate (typically using "foreach") to find all the checkboxes checked.

PM me if there are further questions. I do cover this in my course at OnlinePHPClass.com, and if you are interested, click the WSO link in my signature.


All times are GMT -6. The time now is 06:03 PM.