Jump to content

Working with Select menu in PHP


saikiran

Recommended Posts

hi,

First post in this forum

i am using a drop down select menu to choose a particular category of products from my database table.

I want to give one default option in the select menu, Select ALL, whenever user select this and press the submit button it should retrieve all the records.

kindly check out the below given code and help me out.


Dynamic select menu code


[color=red]<?php include('config.php');?>
<? $query = "SELECT distinct product FROM prod_catlog2";
  $result = mysql_query($query);
 
  $pulldown = '<option>Select one option</option>';
  while($row=mysql_fetch_array($result,MYSQL_ASSOC))
  {
  $pulldown .= "<option value  = \"{$row['product']}\">{$row['product']}</option>\n";
  }?>
 
  <html>
<title>Order Form</title>
<form action="show_ord_det_pg_org.php" method="POST">
Select your category<select name="cat">
<?php echo $pulldown;?> 
</select>
<br>
<input type="submit" name="submit" value="Show Details">
</form>
</html>[/color]


[b]show_ord_det_pg_org.php[/b]

[color=red]...

<?php
  $cat = $_POST['cat'];
  $sql = "select * from prod_catlog2 where product = '$cat'";
  $result = mysql_query($sql)or die(mysql_errno().mysql_error());?> 
....
[/color]

here $cat is the variable which stores the option that user will pick from the select menu, whose name is cat.

kindly help me out

cheers
sai
saisen76@hotmail.com



Link to comment
Share on other sites

<?php include('config.php');?>
<? $query = "SELECT distinct product FROM prod_catlog2";
  $result = mysql_query($query);
 
  $pulldown = '<option>Select one option</option>';
  $pulldown .= '<option>ALL</option>';

  while($row=mysql_fetch_array($result,MYSQL_ASSOC))
  {
      $pulldown .= "<option value  = \"{$row['product']}\">{$row['product']}</option>\n";
  }?>
 
  <html>
<title>Order Form</title>
<form action="show_ord_det_pg_org.php" method="POST">
Select your category<select name="cat">
<?php echo $pulldown;?> 
</select>


<input type="submit" name="submit" value="Show Details">
</form>
</html>

<?php
        if($_POST['cat']=='ALL')
        {$cat='%';}
else
        {$cat = $_POST['cat'];}
        $sql = "select * from prod_catlog2 where product like '$cat'";
        $result = mysql_query($sql)or die(mysql_errno().mysql_error());?>   
Link to comment
Share on other sites

Guest
This topic is now 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.