fran Posted July 14, 2009 Share Posted July 14, 2009 Database Table: contacts id | name | email | c1 | c2 | c3 | notify 1 | Suzie | sue@email.com | a girl | | | Y 2 | John | john@email.com | | a boy | | Y 3 | Uni | uni@email.com | | | 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? Quote Link to comment https://forums.phpfreaks.com/topic/165872-php-question-about-checkboxes/ Share on other sites More sharing options...
ldougherty Posted July 14, 2009 Share Posted July 14, 2009 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' " Quote Link to comment https://forums.phpfreaks.com/topic/165872-php-question-about-checkboxes/#findComment-874921 Share on other sites More sharing options...
fran Posted July 14, 2009 Author Share Posted July 14, 2009 Well, I want to email those who want to be notified but I also want to check off only certain ones. IE: only "a boy" and "a girl" but not "none" as specified by the checked boxes. Quote Link to comment https://forums.phpfreaks.com/topic/165872-php-question-about-checkboxes/#findComment-874922 Share on other sites More sharing options...
pengu Posted July 14, 2009 Share Posted July 14, 2009 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'"; Quote Link to comment https://forums.phpfreaks.com/topic/165872-php-question-about-checkboxes/#findComment-874926 Share on other sites More sharing options...
fran Posted July 14, 2009 Author Share Posted July 14, 2009 Ok, cool. So how do I write the query? $sql = "SELECT email FROM contacts WHERE notify = 'Y' AND sex = 'a boy' AND sex = 'a girl'); Quote Link to comment https://forums.phpfreaks.com/topic/165872-php-question-about-checkboxes/#findComment-874928 Share on other sites More sharing options...
pengu Posted July 14, 2009 Share Posted July 14, 2009 It depends on what you want to do mate. Go through http://w3schools.com/sql/default.asp this to learn SQL queries. Quote Link to comment https://forums.phpfreaks.com/topic/165872-php-question-about-checkboxes/#findComment-874932 Share on other sites More sharing options...
haku Posted July 14, 2009 Share Posted July 14, 2009 Gotta restructure your database: id | name | email | sex | notify 1 | Suzie | sue@email.com | girl | 1 2 | John | john@email.com | boy | 1 3 | Uni | uni@email.com | | 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" Quote Link to comment https://forums.phpfreaks.com/topic/165872-php-question-about-checkboxes/#findComment-874974 Share on other sites More sharing options...
megaresp Posted July 14, 2009 Share Posted July 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/165872-php-question-about-checkboxes/#findComment-874994 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.