Bman900 Posted October 11, 2009 Share Posted October 11, 2009 So I have the following table: newsletter And than I have two categories: email and category I want to have a drop down menu where I can select which category of emails I want to display and send. I have never worked with a drop down menu in PHP so any help would be great. Quote Link to comment https://forums.phpfreaks.com/topic/177257-how-to-use-drop-down-menus-in-php-with-a-database/ Share on other sites More sharing options...
smerny Posted October 11, 2009 Share Posted October 11, 2009 not exactly sure what you're asking, but maybe this? <select name='cat'> <?php $search = "SELECT category FROM newsletter GROUP BY category"; $result = mysql_query($search) or die ('SQL Error:' . mysql_error()); while($row = mysql_fetch_assoc($result)) { echo "<option value='".$row['category']."'>".$row['category']."</option>"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/177257-how-to-use-drop-down-menus-in-php-with-a-database/#findComment-934632 Share on other sites More sharing options...
Bman900 Posted October 11, 2009 Author Share Posted October 11, 2009 I made up something like this but it doesn't actually post anything: <?php if(isset($_POST['submit'])) { $category=$_POST['category']; $result=mysql_query("select email from $category where category= '$category'") or die (mysql_error()); if(mysql_num_rows($result)) { while($row = mysql_fetch_row($result)) { print("<option value=\"$row[0]\">$row[0]</option>"); } } else { print("<option value=\"\">No email in there yet</option>"); } } ?> <form id="form1" name="form1" method="post" action="<? $PHP_SELF; ?>"> <select name="category"> <option>Brazilian Jiu-Jitsu</option> <option>Krav Maga</option> </select> <label> <input type="submit" name="Submit" value="Submit" /> </label> </form> Quote Link to comment https://forums.phpfreaks.com/topic/177257-how-to-use-drop-down-menus-in-php-with-a-database/#findComment-934639 Share on other sites More sharing options...
smerny Posted October 11, 2009 Share Posted October 11, 2009 Not sure what you are doing with the output, as far as I can tell, you don't even have the options within a select? but, start with changing ("select email from $category where category= '$category'") to ("select email from $category where category='".$category."'") Quote Link to comment https://forums.phpfreaks.com/topic/177257-how-to-use-drop-down-menus-in-php-with-a-database/#findComment-934651 Share on other sites More sharing options...
Bman900 Posted October 11, 2009 Author Share Posted October 11, 2009 At this point I just want to print the result for now. Later on I will send emails out to people on that list. Quote Link to comment https://forums.phpfreaks.com/topic/177257-how-to-use-drop-down-menus-in-php-with-a-database/#findComment-934652 Share on other sites More sharing options...
smerny Posted October 11, 2009 Share Posted October 11, 2009 <?php if(isset($_POST['submit'])) { $category=$_POST['category']; $result=mysql_query("select email from $category where category= '".$category."'") or die (mysql_error()); if(mysql_num_rows($result)) { while($row = mysql_fetch_row($result)) { echo $row['category']."<br />"; } } else { echo "No email in there yet"; } } ?> <form id="form1" name="form1" method="post" action="<? $PHP_SELF; ?>"> <select name="category"> <option>Brazilian Jiu-Jitsu</option> <option>Krav Maga</option> </select> <label> <input type="submit" name="Submit" value="Submit" /> </label> </form> how's this work for you? Quote Link to comment https://forums.phpfreaks.com/topic/177257-how-to-use-drop-down-menus-in-php-with-a-database/#findComment-934658 Share on other sites More sharing options...
Bman900 Posted October 11, 2009 Author Share Posted October 11, 2009 Ok something is not going right because I am not passing on the value of the selection. Here is new new simplified code: <?php mysql_connect("db2075.perfora.net","dbo300131695","WKPRzTpp"); mysql_select_db("db300131695"); if(isset($_POST['submit'])) { $category=$_POST['category']; echo "$category"; } ?> <form id="form1" name="form1" method="post" action="<? $PHP_SELF; ?>"> <select name="category"> <option>Brazilian Jiu-Jitsu</option> <option>Krav Maga</option> </select> <label> <input type="submit" name="Submit" value="Submit" /> </label> </form> Quote Link to comment https://forums.phpfreaks.com/topic/177257-how-to-use-drop-down-menus-in-php-with-a-database/#findComment-934674 Share on other sites More sharing options...
Bman900 Posted October 11, 2009 Author Share Posted October 11, 2009 Ok lets say I have the following: email | category [email protected] A [email protected] A [email protected] B How do I select and display entries where the category is A only? Quote Link to comment https://forums.phpfreaks.com/topic/177257-how-to-use-drop-down-menus-in-php-with-a-database/#findComment-934678 Share on other sites More sharing options...
smerny Posted October 11, 2009 Share Posted October 11, 2009 Ok something is not going right because I am not passing on the value of the selection. Here is new new simplified code: <?php mysql_connect("db2075.perfora.net","dbo300131695","WKPRzTpp"); mysql_select_db("db300131695"); if(isset($_POST['submit'])) { $category=$_POST['category']; echo "$category"; } ?> <form id="form1" name="form1" method="post" action="<? $PHP_SELF; ?>"> <select name="category"> <option>Brazilian Jiu-Jitsu</option> <option>Krav Maga</option> </select> <label> <input type="submit" name="Submit" value="Submit" /> </label> </form> try changing the submit button name to "submit" (lowercase to match the isset check)... and when you are echoing variables, don't put them in quotations... so echo $category; instead of echo "$category"; Quote Link to comment https://forums.phpfreaks.com/topic/177257-how-to-use-drop-down-menus-in-php-with-a-database/#findComment-934851 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.