Jump to content

PHP question about checkboxes


fran

Recommended Posts

Database Table: contacts

 

id | name |        email          |    c1    |  c2      |    c3    |    notify

1 |  Suzie | [email protected]  |  a girl  |            |            |    Y

2 |  John  | [email protected]  |          |  a boy  |            |    Y

3 |  Uni    | [email protected]    |          |            |  none  |    Y

 

form:

 

Send email to this type of people:

 

<input type=checkbox name=c1 value="a girl">

<input type=checkbox name=c2 value="none">

<input type=checkbox name=c3 value="a boy">

 

PHP:

 

$sql = "SELECT email FROM contacts WHERE notify = 'Y' AND (checkbox is checked);

 

 

How can I specify the fields that are checked?

Link to comment
https://forums.phpfreaks.com/topic/165872-php-question-about-checkboxes/
Share on other sites

I'm not sure I'm understanding.  If the data is already in the database ie notify field is Y meaning they wish to be notified then when sending emails or at least generating the email list you should just use

 

$sql = "SELECT email FROM contacts WHERE notify = 'Y' "

Why not remove C1 C2 and C3 and call it sex. Then you can have "male" or "female"

 

Then change your options.

<form action="email-form.php" method="post">
<input type="checkbox" name="sex" value="female">Female
<input type="checkbox" name="sex" value="male">Male
</form>

 

$sql = "SELECT email FROM contacts WHERE notify = 'Y'  AND sex = 'male'";

Gotta restructure your database:

 

id | name |        email          |  sex    |    notify

1 |  Suzie | [email protected]  |  girl  |      1

2 |  John  | [email protected]  |  boy  |      1

3 |  Uni    | [email protected]    |          |      1

 

If you want to get the girls who want to be notified:

 

"SELECT email FROM contacts WHERE sex = 'girl' AND notify = 1"

 

If you want boys who want to be notified:

 

"SELECT email FROM contacts WHERE sex = 'boy' AND notify = 1"

 

If you want both sexes, you use

 

"SELECT email FROM contacts WHERE(sex = 'boy' OR sex = 'girl') AND notify = 1"

If you want both sexes, you use

 

SELECT email FROM contacts WHERE(sex = 'boy' OR sex = 'girl') AND notify = 1

 

I've included a shorter version of the query because it's useful shorthand, especially when you have more then two OR alternatives:

 

SELECT email FROM contacts WHERE sex in ('boy','girl') AND notify = 1

 

You might also consider making sex a char(1) field and limit it to M or F ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.