Jump to content

Recommended Posts

I have three table.Category,sub category and company.

I populate two drop down.when select category(1st drop down) it will populate 2nd drop down(sub category)

What I want when I will select 2nd drop down(sub category) corresponding company details will come from company table.

is it possible in PHP

following are the codes for two drop down

 

index.php

<?php
include("Config.php");
$sql = "SELECT * FROM category ORDER BY CategoryDetails ASC";
$countries = $dbo->query($sql);
?>


<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>Dropdown list example</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
      <link rel="stylesheet" href="style.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
      <script>
         function getStates(CategoryID){
            $("#state").show();
            $("#stateDropdown").html('<option>Loading...</option>');
            $.ajax({
               method: "POST",
               url: "getStates.php",
               dataType: "html",
               data: {category: CategoryID}
            })
               .done(function(data){
               $("#stateDropdown").html(data);
            });
         }  
         
      </script>
   </head>
   <body>
       <style>#state {
   display: none;
}</style>
      <div class="container">
         <form class="form-horizontal">
            <div class="form-group">
               <label for="category" class="col-sm-2 control-label">Category:</label>
               <div class="col-sm-4">
                  <select class="form-control" id="category" onChange="getStates(this.value)">
                     <option value="">Select Category</option>
                     <?php
                     foreach($countries as $category){
                        echo "<option value='" . $category['CategoryID'] . "'>" . $category['CategoryDetails'] . "</option>";
                     }
                     ?>
                  </select>
               </div>
            </div>
            <div class="form-group" id="state">
               <label for="state" class="col-sm-2 control-label">Sub Category:</label>
               <div class="col-sm-4">               
                  <select class="form-control" id="stateDropdown">
                  </select>
               </div>
            </div>
         </form>
      </div>
   </body>
</html>

getstates.php

 

<?php
if(!empty($_POST['category'])){
   require("Config.php");
   $CategoryID = $_POST['category'];
   $sql = "SELECT * FROM subcategory WHERE CategoryID = :CategoryID ORDER BY SCategoryDetails";
   $states = $dbo->prepare($sql);
   $states->execute(array(
      ':CategoryID'=> $CategoryID
   ));
   echo "<option>Select Subcategory...</option>";
   foreach($states as $subcategory){
      echo "<option value='" . $subcategory['SCategoryID'] . "'>" . $subcategory['SCategoryDetails'] . "</option>";
   }
}
?>

config.php

 

<?Php
/////// Update your database login details here /////
$dbhost_name = "localhost"; // Your host name 
$database = "leadforce";       // Your database name
$username = "gb";            // Your login userid 
$password = "123";            // Your password 
//////// End of database details of your server //////


//////// Do not Edit below /////////
try {
$dbo = new PDO('mysql:host='.$dbhost_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?> 

and also sample page i enclose, what actually i want

 

if anybody help

 

Thanks

 

post-199278-0-59140300-1469027634_thumb.jpg

  • 2 weeks later...
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.