Jump to content

How to use drop down menus in php with a database?


Bman900

Recommended Posts

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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."'")

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.