Page 1 of 1

PHP Check boxes

Posted: Fri Jan 18, 2008 8:36 am
by webtech123
Hi all, I was looking to use check boxes in a html form for a registration page.

HTML Code:
<input type="checkbox" name="Industry" value="Ecommerce" /></input><label for="Ecommerce">Ecommerce</label>
<input type="checkbox" name="Industry" value="IT - Web Design" /></input><label for="IT - Web Design">IT - Web Design</label>

PHP Code
$insert = mysql_query("insert into $table values ('NUll',
'".$_POST['CompanyName']."',
'".$_POST['Address']."',
'".$_POST['Town']."',
'".$_POST['PostCode']."',
'".$_POST['Telephone']."',
'".$_POST['Fax']."',
'".$_POST['Url']."',
'".$_POST['Email']."',
'".$_POST['Industry']."',
'".$_POST['Description']."')")
or die ("Could not insert ".mysql_error());

the mysql all i see is "array".

Any help would be great

many thanks
James

Re: PHP Check boxes

Posted: Fri Jan 18, 2008 8:38 am
by VladSun
I think you need a radio buttons instead of check boxes ...

Re: PHP Check boxes

Posted: Fri Jan 18, 2008 8:40 am
by JayBird
I spent all that time writing this tutorial and noone reads it :(

Re: PHP Check boxes

Posted: Fri Jan 18, 2008 8:41 am
by webtech123
Thanks for your reply. What if i wanted multiple answers? The checkboxes are to register your industry interest(s), some have more than one.

James

Re: PHP Check boxes

Posted: Fri Jan 18, 2008 8:46 am
by VladSun
Then you'll have to normalize your tables.
Read this http://www.devshed.com/c/a/MySQL/An-Int ... alization/

Re: PHP Check boxes

Posted: Fri Jan 18, 2008 9:00 am
by thinsoldier
VladSun wrote:Then you'll have to normalize your tables.
Always a good thing to do but for quick and simple projects you can get away with just saving a comma separated string to a text field in your mysql table.

Changes:
<input type="checkbox" name="industries[]" value="Ecommerce" /></input><label for="Ecommerce">Ecommerce</label>
<input type="checkbox" name="industries[]" value="IT - Web Design" /></input><label for="IT - Web Design">IT - Web Design</label>

now when you submit the form if you do <pre><? print_r($_POST); ?></pre> you'll see there's a nice 'industries' array with only the industries that were checked. You can turn that array into a comma separated list of values and save it all in 1 field in your table.

I've you're going to need to edit existing data with this form I'd suggest having another table with all possible industry areas and using the id of those records as the values of the checkboxes.

Re: PHP Check boxes

Posted: Fri Jan 18, 2008 2:59 pm
by Jonah Bron
Also an XHTML syntax note:
There are two types of tags;
singular, and surrounding. Input tags are the singular type. Same with IMGs. Divs are surrounding. Same with tables, etc.

Singular ex:

Code: Select all

<input type="text" name="text-box" value="Type into this box" />
Surrounding ex:

Code: Select all

<div style="width: 80%">Text here</div>
:D