PHP Check boxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
webtech123
Forum Newbie
Posts: 2
Joined: Fri Jan 18, 2008 8:32 am

PHP Check boxes

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP Check boxes

Post by VladSun »

I think you need a radio buttons instead of check boxes ...
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Re: PHP Check boxes

Post by JayBird »

I spent all that time writing this tutorial and noone reads it :(
webtech123
Forum Newbie
Posts: 2
Joined: Fri Jan 18, 2008 8:32 am

Re: PHP Check boxes

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP Check boxes

Post by VladSun »

Then you'll have to normalize your tables.
Read this http://www.devshed.com/c/a/MySQL/An-Int ... alization/
There are 10 types of people in this world, those who understand binary and those who don't
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: PHP Check boxes

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Check boxes

Post 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
Post Reply