2016-12-03 69 views
0

我有两个下拉菜单中的一个是&其他的是产品。我想从类别中选择价值,因为我想在产品下拉列表中显示价值。下面从下拉菜单中选择一个值来显示另一个下拉值中的值在php

是我的代码

<!-- <?php 
// include('session.php'); 
?> --> 
<?php 
mysql_connect('localhost', 'root', ''); 
mysql_select_db('product_management'); 

$sql = "SELECT * FROM category"; 
$result = mysql_query($sql); 



$sql1 = "SELECT * FROM category.c INNER JOIN products.p where WHERE  category_id.p = '$id' "; 
$result1 = mysql_query($sql1); 
?> 


<!DOCTYPE html> 
<html> 
<head> 
<title>Product Page</title> 

</head> 
<body> 

<div class="container-fluid"> 
<div class="row"> 
    <div class="col-md-12" style="margin-left: 30%;margin-top: 10%;"> 
     <form role="form"> 

      <div class="form-group"> 

       <label> 
        Category 
       </label> 
       <select name='category_type' id="category_type"> 
       <?php 
while ($row = mysql_fetch_array($result)) { 
echo "<option value='" . $row['category_id'] ."'>" . $row['category_type'] ."</option>"; 
} 
?> 
</select> 
      </div> 

      <div class="form-group">      
       <label> 
        Products 
       </label> 
       <select id="products" name="products" required> 
        <option value=""> -- SELECT -- </option> 
        <?php 
while ($row = mysql_fetch_array($result1)) { 
echo "<option value='" . $row['products'] ."'>" . $row['products'] ."</option>"; 
} 
?> 
       </select> 
      </div> 

      <div class="form-group">      
       <label> 
        Price 
       </label> 
       <input type="text" id="price" name="price" required> 
      </div> 

      <div class="form-group">      
       <label> 
        Quantity 
       </label> 
       <select id="quantity" name="quantity" required> 
        <option value=""> -- SELECT -- </option> 
       </select> 
      </div> 


      <div class="form-group"> 

       <label for="exampleInputFile"> 
        Upload Image 
       </label> 
       <input id="exampleInputFile" type="file" />     
      </div> 



      <button type="submit" class="btn btn-default">Submit</button> 
     </form> 
    </div> 
</div> 

如何实现这一目标?我想从下拉菜单中选择一个值来显示另一个下拉值中的值。

+0

你可以通过调用ajax来实现。 – vel

+0

任何示例plz? – Kirataka

+0

http://stackoverflow.com/questions/34961758/change-dropdown-value-when-another-dropdown-selected-in-a-form-using-jquery-aj – vel

回答

1

你需要在你的代码的一些变化,如:

  • 变化以下方式category_type下拉列表中,

    <select name='category_type' id="category_type"> 
        <option value=""> -- SELECT -- </option> 
    <?php 
        while ($row = mysql_fetch_array($result)) { 
         echo "<option value='" . $row['category_id'] ."'>" . $row['category_type'] ."</option>"; 
        } 
    ?> 
    </select> 
    

    ,并创建一个空的下拉列表商品这样的商品:

    <select id="products" name="products" required> 
        <option value=""> -- SELECT -- </option> 
    </select> 
    
  • 现在使用AJAX根据选定的category_type填充产品下拉列表。创建一个单独的get_products.php页面来处理AJAX请求。您可以使用以下jQuery/AJAX代码片段将选定的category_id发送到该页面。

    <script> 
        $(document).ready(function(){ 
         $(document).on('change', '#category_type', function(){ 
          var category_id = $(this).val(); 
          if(category_id.length != 0){ 
           $.ajax({ 
            type: 'POST', 
            url: 'get_products.php', 
            cache: 'false', 
            data: {category_id:category_id}, 
            success: function(data){ 
             $('#products').html(data); 
            }, 
    
            error: function(jqXHR, textStatus, errorThrown){ 
             // error 
            } 
           }); 
          }else{ 
           $('#products').html('<option value=""> -- SELECT -- </option>'); 
          } 
         }); 
        }); 
    </script> 
    

    而且在get_products.php页面,流程以下列方式你的AJAX请求,

    <?php 
    
        if(isset($_POST['category_id'])){ 
         // your database connection code 
    
         $id = $_POST['category_id']; 
         $sql = "SELECT * FROM products WHERE category_id = '$id' "; 
         $result = mysql_query($sql); 
         while($row = mysql_fetch_array($result)){ 
          echo "<option value='" . $row['product_id'] ."'>" . $row['product_type'] ."</option>"; 
         } 
        } 
    
    ?> 
    

    变化$row['product_id']$row['product_type']按你的表的属性名称。


旁注:不要使用mysql_数据库扩展,他们不赞成PHP 5.5.0,并在PHP 7.0.0完全删除。改为使用mysqliPDO驱动程序。这是why you shouldn't use mysql_* functions

+0

if(isset($ _ POST ['category_id']) )){ \t $ con = mysqli_connect(“localhost”,“root”,“”,“product_management”); (mysqli_connect_errno()) echo“无法连接到MySQL:”。 mysqli_connect_error(); } – Kirataka

+0

我在我的ajax页面中使用它stil没有错误或结果即将到来 – Kirataka

+0

@Kirataka您是否更改了'url:'get_products.php','根据您的页面设置?另外,在你的AJAX中也显示错误信息,如下所示:'error:function(jqXHR,textStatus,errorThrown){alert(errorThrown); }我使用了' –

相关问题