Html Check boxes and PHP

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
DougieC
Forum Newbie
Posts: 18
Joined: Fri Jul 25, 2003 1:49 pm

Html Check boxes and PHP

Post by DougieC »

I am trying to make a form with multiple check boxes that can refine a mysql query search. When i try to pass it to the php script with a post, it only passes the last value checked. I'm new to all this so I am not even sure if I am doing it right :oops:
here is the html checkbox code
<input name="services" type="checkbox" value="*" checked>
<input name="services" type="checkbox" value="option1"> <input name="services" type="checkbox" value="option2"> <input name="services" type="checkbox" value="option3">

and the php
$services = $_POST['services'];
do i need to make $services an array, and if so would that be @services.

Thanks for the help
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

i'm not sure if it's html or php here, but you're trying to pass a potential array, and arrays are signaled in php and most other scripting/programing languages i have encountered with [] at the end when you're talking about the array in general.

php is trying to use a string type. becasue of this, you only get the final value. this can be fixed by changing the name from services to services[]

however, this will ONLY give the checked values. be sure that's the functionality you want prior to using it. you may want a series of radio buttons instead.
jmarcv
Forum Contributor
Posts: 131
Joined: Tue Jul 29, 2003 7:17 pm
Location: Colorado

Post by jmarcv »

Yes, you need to make it an array, like so.

<input name="services[]" type="checkbox" value="*" checked>
<input name="services[]" type="checkbox" value="option1"> <input name="services[]" type="checkbox" value="option2"> <input name="services[]" type="checkbox" value="option3">
Post Reply