Link to home
Start Free TrialLog in
Avatar of epaschal
epaschalFlag for United States of America

asked on

Translating Checkboxes to MySQL / PHP Code

Hello Experts,

I have a search form used to locate missing persons reports matching certain criteria, and am adding a section with checkboxes to examine field "eyecolor". I am using a shorter version for this question.

The boxes on the form are:
1> All
2> Black
3> Blue
4> Brown

In the database, black is stored as "1", blue as "2", and Brown as "3"

How can I write the PHP and SQL query to handle this on the backend? When I tried to write it, I can't account for how to add the "OR" between multiple selction in the SQL, or how to not list "OR" if only one item is chosed.

The added twist is  - I need for "all" to somehow negate any selections and not add specification to the query.

Thanks in advance for any help or guidance you can provide,

Eric

Avatar of quad341
quad341

for negation, you can either turn to javascript (insecure, unreliable) or server side testing (would work fine) where you test if all is set, and if so, ignore everything else.

i'm not sure how your database stores requests that have multiple answers.  I personally, since there is a very limited amount of responses, would set up a single character value to store say blue and black as 5 so you can store it as char(1) in your database.

also, you have to make sure you're using different names for your checkboxes or else the server side script will only see the last value to be set.  you could loop through the variables using a foreach loop or manually set them.

You could also chose to write out all the choices and just use a radio button.  then only one choice could be selected at a time and you would always know the value and not need to do much strange checking, etc.
ASKER CERTIFIED SOLUTION
Avatar of ldbkutty
ldbkutty
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of epaschal

ASKER

I apologize for not noting this in my original message, but each record has only one eye color listed.

The purpose of the checkboxes is to allow people to return records having any of the selected eye colors, or if they select "all" - to not apply the eye color filter at all.

>> The purpose of the checkboxes is to allow people to return records having any of the selected eye colors, or if they select "all" - to not apply the eye color filter at all.

Thats what my code does, Have you tried it ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ldbkutty and jdpipe,

Thank you for providing such helpful suggestions.

I am going to work with this now and will post back soon.

Eric