2016-07-27 163 views
0

我使用ajax來做我的選擇框,但是當我選擇第一個選擇框時,第二個選擇框沒有顯示任何值。如何顯示第二個選擇框中的值?基於另一個選擇框值的選擇框php

的index.php(jQuery的):

$(document).ready(function(){ 
    $('#brand').on('change',function(){ 
     var brand = $(this).val(); 
     if(brand){ 
      $.ajax({ 
       type:'POST', 
       url:'ajax_city.php', 
       data:'brand='+brand, 
       success:function(html){ 
        $('#outlet').html(html); 
       } 
      }); 
     }else{ 
      $('#outlet').html('<option value="">Select OUTLET first</option>'); 
     } 
    }); 
}); 

的index.php(HTML/PHP)

 <select class="brand" style="width:200px" id="brand" name="brand" > 
      <?php $i = 0; 
      while (!$br->EOF) { 
       $fv = $br->Fields("mBrand"); 
       $name = $fv->value; 
       echo '<option value="' . trim($name) . '"><b>' . $name . '</b></option>'; 
       $br->MoveNext(); 
      } 
      ?> 
     </select> 
     <input type="hidden" name="loc" id="loc"> 
    </td> 
</div> 
<li class="form-line" id="id_19"> 
    <label class="form-label-left" id="label_19" for="input_19"> Outlet </label> 
    <div id="cid_20" class="form-input"> 
    <br><br> 
    <select class="outlet" name="outlet" id="outlet" style="width:200px" > 
     <option value="">--Select outlet--</option> 
    </select> 

ajax_city.php:

if(isset($_POST["brand"])&&!empty($_POST["brand"])) 
{ 
$brand=$_POST['brand']; 
$rb = $itdeptconn->Execute("SELECT DISTINCT mOutlet FROM [IT_Dept].[dbo].[mstOutlet] WHERE mBrand='".$brand."'"); 
//$sql=mysql_query("select b.id,b.data from data_parent a,data b where b.id=a.did and parent='$id'"); 

echo '<option value="">Select Outlet</option>'; 
           while (!$rb->EOF) { 

            $fv = $rb->Fields("mOutlet"); 
            $name = $fv->value; 

            echo '<option value="' . trim($name) . '"><b>' . $name . '</b></option>'; 
            $rb->MoveNext(); 
           } 
} 
       ?> 
+0

任何控制檯錯誤? – Rasclatt

+0

在你的ajax中,試試:'data:{「brand」:brand},' – Rasclatt

+0

@Rasclatt沒有錯誤顯示。 – Jazlyn

回答

0

ü請使用方括號而不是ajax_city.php頁面上的圓括號。 它應該是 - >空($ _ POST [ 「品牌」))

希望它會工作

+0

我已經嘗試更換支架,但仍然沒有工作。 – Jazlyn

+0

您錯過了ajax_city.php上的一個右括號,添加那個右括號並再試一次 – Roma

+0

嗨,我已添加了右括號,但下拉框的值也沒有出來。 – Jazlyn

0

指數:

       <!DOCTYPE html> 
      <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'> 
       <head> 
       <meta http-equiv='content-type' content='text/html;charset=utf-8' /> 
       <link type='text/css' rel='stylesheet' href='test.css' /> 
       <script type='text/javascript' src='js/jquery-1.12.1.js'></script> 
       </head> 
      <body> 
      <select class="brand" style="width:200px" id="brand" name="brand" > 
         <option value="">--Select outlet--</option> 
        <option value="data11">Data11</option> 
        <option value="data21">data21</option> 
        <option value="data31">data31</option> 
        </select> 
        <input type="hidden" name="loc" id="loc"> 
       </td> 
      </div> 
      <li class="form-line" id="id_19"> 
       <label class="form-label-left" id="label_19" for="input_19"> Outlet </label> 
       <div id="cid_20" class="form-input"> 
       <br><br> 
       <select class="outlet" name="outlet" id="outlet" style="width:200px" > 
        <option value="">--Select outlet--</option> 
       </select> 
       <script type="text/javascript"> 
       $(document).ready(function(){ 
        $('#brand').on('change',function(){ 
         var brand = $(this).val(); 
         if(true){ 
          $.ajax({ 
           type:'POST', 
           url:'ajax_city.php', 
           data:'brand='+brand, 
           success:function(html){ 
            $('#outlet').html(html); 
           } 
          }); 
         }else{ 
          $('#outlet').html('<option value="">Select OUTLET first</option>'); 
         } 
        }); 
       }); 
       </script> 
      </body> 
      </html> 

ajax_city.php

       <?php 

      if(isset($_POST["brand"])&&!empty($_POST["brand"])) 
      { 
       $brand = $_POST['brand']; 
      // $rb = $itdeptconn->Execute("SELECT DISTINCT mOutlet FROM [IT_Dept].[dbo].[mstOutlet] WHERE mBrand='".$brand."'"); 
       //$sql=mysql_query("select b.id,b.data from data_parent a,data b where b.id=a.did and parent='$id'"); 
       $rb = ["brand1","brand2","brand3"]; 
      // $rowcount = $rb->num_rows; 
       if(count($rb)>0){ 
        echo '<option value="">Select Outlet</option>'; 
         for($i=0;$i<count($rb);$i++) 
         { 
          echo '<option value="'.$rb[$i] .'"><b>'.$rb[$i]. '</b></option>'; 
         } 
       }else { 
        echo '<option value="">Outlet not available</option>'; 
       } 
      } 
      ?> 
+0

我可以這樣寫嗎?我已發佈我的答案在另一篇文章 – Jazlyn

0

使用這個代碼,我已經添加了所有的評論,然後檢查它是否有效。它對我來說工作得很好。

ajax_city.php

   <?php 
      define("DBHOST"," "); //write your host name 
      define("DBNAME"," "); //write your database name 
      define("DBUSER"," "); //write your username 
      define("PASS"," "); //write your password 
      $itdeptconn = new PDO('mysql:host='.DBHOST.';dbname='.DBNAME,DBUSER,PASS); 


      if(isset($_POST["brand"])&&!empty($_POST["brand"])) 
      { 
       $brand=$_POST['brand']; 
       $rb=$itdeptconn->prepare("SELECT DISTINCT name FROM restaurant WHERE check_payable_to='C3'"); //write here your query 
       $rb ->execute(); 
       if(count($rb)>0) 
       { 
        echo '<option value="">Select Outlet</option>'; 
         while($data = $rb ->fetch()) 
         { 
          echo '<option value="' . $data['name'] . '"><b>' . $data['name'] . '</b></option>'; } //name is the column name,change it with your column name 
         } 
       }  
       else 
       { 
        echo '<option value="">Outlet not available</option>'; 
       }  
      ?> 
+0

感謝您的代碼,但這項工作oledb數據庫? bcoz即時通訊使用它。 – Jazlyn

相關問題