Jump to content

Recommended Posts

I need a code  that can help me to create 3 dependable dropdown selects form from a "products" table in mysql database.

 

Please see attached screenshot for my "products" table.

 

I need a dependabe dropdown  select box for my form ie-- if a "category" item is selected the corresponding "1st_subcategory" appears and based on "1st_subcategory" selected the "2nd_subcategory" items appears.

 

Not sure how to approach this. I have been struggling with this for 3-4 days. Please help.

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/282086-dependable-dropdown-menu-he/
Share on other sites

Hi.

 

kindly check this http://forums.phpfreaks.com/topic/281154-3-dependent-drop-down-list/  you found out the solution in the last row of the discussion.

hi,

 

thank you for your quick response, however I am still not clear on the solution.I use table for my form...

 

my html is below and products table screenshot also attached - Please let me know how i can get my dropdown for category ,fist subcategory and second category to work

 

 

<?php
session_start(); 
if (!isset($_SESSION["manager"])){
header("location:admin_login.php");
exit();
}
?>
<?php
/* checking if this is a valid session in database  */
$managerID=preg_replace('#[^0-9]#','',$_SESSION["id"]);
$username=preg_replace('#[^0-9a-z]#i','',$_SESSION["manager"]);
$password=preg_replace('#[^0-9a-z]#i','',$_SESSION["password"]);
 
include "../storescripts/connect_to_mysql.php";  /* connect to database */
/* do a query on database */
$sql=mysql_query("SELECT * FROM admin WHERE id='$managerID' AND username='$username' AND password='$password'");
$row=mysql_num_rows($sql);/* counting the number of rows returned from above query */
 
if($row==0)
{ echo "You are not authorised to enter this region as your credentials are not valid on our database";
exit(); }
?>
<?php
/* list all the products for viewing*/
$product_list="";
$sql=mysql_query("SELECT * FROM products");
$count=mysql_num_rows($sql);
if ($count>0)
{ while($row = mysql_fetch_array($sql)){
       $id=$row["id"];
  $product_name=$row["product_name"];
  $price=$row["price"];
  $category=$row["category"];
  $first_subcategory=$row["first_subcategory"];
  $second_subcategory=$row["second_subcategory"];
  $date_added=$row["date_added"];
  
$product_list .= "$id- $product_name- $price- $category- $first_subcategory- second_subcategory- $date_added  <a href='#'>edit</a> •<a href='#'>delete</a> <br>";
}
}
else
{
$product_list = "You dont have any products listed in your store yet";
}
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Manage Inventory</title>
<link rel="stylesheet" href="../style/style.css" type="text/css" media="screen"/>
</head>
 
<body >
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.category.options[form.category.options.selectedIndex].value;
self.location='addrequest.php?category=' + val ;
}
function reloadtwo(form)
{
var val=form.category.options[form.category.options.selectedIndex].value;
var val1=form.first_subcategory.options[form.first_subcategory.options.selectedIndex].value;
self.location='addrequest.php?category=' + val + '&first_subcategory=' + val1 ;
}
</script>
 
<?php
 
@$category = $_GET['category']; 
@$first_subcategory = $_GET['first_subcategory']; 
if(strlen($category) > 0 and is_numeric($category))
{
echo "Query Error";
exit;
}
elseif(strlen($first_subcategory) > 0 and is_numeric($first_subcategory))
{
echo "Query Error";
exit;
}
 
$quer2 = mysql_query("SELECT DISTINCT category FROM products"); 
 
if(isset($category) and strlen($category) > 0)
{
$quer = mysql_query("SELECT DISTINCT first_subcategory, category FROM products WHERE category = '$category'");
}
 
 
 
$quer3 = mysql_query("SELECT DISTINCT first_subcategory, category FROM products WHERE category = '$category'"); 
 
if(isset($first_subcategory) and strlen($first_subcategory) > 0)
{
$quer33 = mysql_query("SELECT DISTINCT second_subcategory,first_subcategory,category FROM products WHERE category = '$category' and first_subcategory = '$first_subcategory'"); 
}
 
 
?>
........................
        <div id= "pageContent">
  <h1 align="center"> Manage Inventory</h1>
  <div align= "right" style=" margin: 50px" ><a href="inventory_list.php#inventoryForm">+Click here to add inventory to the store </a></div>
  <p><?php echo $product_list; ?></p>
  <a name="inventoryForm" id="inventoryForm"></a>
  <h2 align="center"> Add  Inventory Items to the store</h2>
  <form action="inventory_list.php" method="post" enctype="multipart/form-data" name="myForm"><table width="87%" border="1">
    <tr>
      <td align="right" width="29%">Product Name </td>
      <td width="43%"><input name="product_name" type="text" id="product_name"/></td>
      <td width="28%"><label id="product_name" > </label></td>
    </tr>
    <tr>
      <td align="right">Price  </td>
      <td> $ <input name="price" type="text" id="price"/></td>
      <td><label id="price"> </label></td>
    </tr>
    <tr>
      <td align="right">Category  </td>
      <td><select name="category" id="category">
      <option value=""></option>
      <option value="Clothing">Clothing</option>
      <option value="Electronics">Electronics</option>
      <option value="Automobiles">Automobiles</option>
      </select></td>
      <td><label id="category"> </label></td>
    </tr>
    <tr>
      <td align="right">Subcategory  </td>
      <td> <input name="first_subcategory" type="text" id="subcategory"/></td>
      <td><label id="subcategory"> </label></td>
    </tr>
     <tr>
      <td align="right">Second Subcategory  </td>
      <td> <input name="second_subcategory" type="text" id="second_subcategory"/></td>
      <td><label id="second_subcategory"> </label></td>
    </tr>
    <tr>
      <td align="right">Details   </td>
      <td><textarea name="details" cols="55" rows="10"> </textarea></td>
      <td><label id="details"> </label></td>
    </tr>
    <tr>
      <td align="right">Upload Product Image  </td>
      <td> <input name="imagefile" type="file" /></td>
      <td><label id="imagefile"> </label></td>
    </tr>
    <tr>
      <td> </td>
      <td><input name="submit" type="submit" value="submit" />  <input name="reset" type="reset" value="reset" /></td>
      <td> </td>
    </tr>
  </table></form>
  <p> </p>
  <p> </p>
 
</body>
</html>

I am new to php...and since i am using tables i am not sure how to get the select boxes to display in the table.

 

 

if you look at my form table the category,first_subcategory,and second_subcategory section is not written properly.need to link the php code to get the select boxes to display. As of now it just displays only the categories section dropdown as in the html code.The other 2 dropdowns are not populating. 

 

Thanks again for looking into this one for me.

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.