2016-07-27 6 views
0

Ich benutze Ajax, um meine Auswahlbox zu machen, aber die zweite Auswahlbox zeigte keinen Wert, wenn ich die 1. Auswahlbox wähle. Wie kann ich den Wert in der 2. Auswahlbox anzeigen?Auswahlbox basierend auf einem anderen Auswahlfeld Wert 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

Alle Fehler-Konsole? – Rasclatt

+0

Versuchen Sie in Ihrem Ajax: 'data: {" brand ": brand},' – Rasclatt

+0

@Rasclatt keine Fehleranzeige. – Jazlyn

Antwort

0

U s eckige Klammern anstelle von runden Klammern auf Ihrer ajax_city.php Seite. Es sollte -> empty ($ _ POST [ "Marke"]))

hoffen, dass es

+0

Ich habe versucht, die Klammer zu ändern, aber immer noch nicht funktioniert. – Jazlyn

+0

Sie haben eine schließende Klammer auf ajax_city.php verpasst, fügen Sie diese schließende Klammer hinzu und versuchen Sie es wieder – Roma

+0

hi, ich habe addieren Sie zurück die schließende Klammer, aber der Drop-down-Kasten Wert kam nicht auch heraus. – Jazlyn

0

Index arbeiten:

       <!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

kann ich so schreiben? Ich habe meine Antwort auf einen anderen Beitrag – Jazlyn

0

Verwenden Diesen Code habe ich hinzugefügt, alle Kommentare folgen nur diesen Kommentaren und überprüfen, ob es funktioniert. Es funktioniert gut für mich.

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

Danke für den Code, aber diese Arbeit für oledb-Datenbank? bcoz ich benutze es. – Jazlyn