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.

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>

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>

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

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

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>

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

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.