2012-08-30 153 views
0

有兩個下拉菜單, A和B.自動填充兩個下拉菜單

價值組合是: - 汽車,自行車,飛機 的值B是(在選擇汽車): - ferari,奔馳 當自行車被選中: - 杜卡迪,哈雷 當平面選擇: - MIG,協和

來自投遞菜單中的價值選擇同樣以後,隨着價格的文本框將被更新,我需要這是一個循環過程。我如何實現它?我對PHP相當陌生。

+1

你到目前爲止做了什麼? – diEcho

+0

[Cascade Dropdown List using jQuery/PHP]可能的重複(http://stackoverflow.com/questions/7137357/cascade-dropdown-list-using-jquery-php) – podiluska

+2

我想你想做出動態的選擇框,關注鏈接:http://blog.webtech11.com/2012/03/04/dynamic-select-box-with-php-and-jquery.html –

回答

0

首先我要感謝jogesh_p的幫助。

我終於設法自動填充下拉菜單,並取決於第一個下拉菜單製作第二個下拉菜單。

由於jogesh提到他的博客Dynamic select box with php and jQuery

也有很大的幫助。

但是,我將發佈我已經使用的代碼,因爲論壇傾向於在Google中佔據更高的結果,並使其更容易爲其他像我這樣的新PHP程序員找到。

編碼在兩個頁面完成:

  1. attempt.php(這是主要的頁面)
  2. level.php(當第一個下拉菜單中的值被改變了這個頁面被稱爲)

jQuery和PHP已被使用。

attempt.php

<?php 
//connect to the database 
$con = mysql_connect("localhost","root","12345") or die("error ".mysql_error()); 

//connect to the trav table 
mysql_select_db("trav",$con) or die("error ".mysql_error()); 
?> 

<html> 
<head>  
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    <script> 
    $(function() 
    { 

     $(window).load(function() 
     { 
      $('#building').change(function() 
      { 
       var parentValue = $(this).val(); 


       // Determine the selected Value 
       if(parentValue.length != "") 
       { 
        $.ajax(
        { 
         url: 'level.php', 
         data: {parent: parentValue}, 
         success: function(data) 
         { 
          $('#level').html(data); 
         } 
        }); 
       } 
       else 
       { 
        $('#level').html('<option value="">Please Select</option>'); 
       } 
      }); 
     }); 
    }); 
    </script> 
</head> 

<body> 
<form method="post" action="<?php echo $PHP_SELF;?>"> 

    <table cellpadding="6" cellspacing="1"> 
     <tr> 
      <td> 
       <label>Select Building : </label> 
      </td> 
      <td> 
       <select id="building"> 
        <option value="">Please Select</option> 
        <?php 
         $query = "select * from build_list"; 
         $result = mysql_query($query) or die('Error'.mysql_error()); 

         while($row = mysql_fetch_assoc($result)) 
         { 
          echo '<option value="'. $row['building'] .'">' . ucwords($row['building']) . '</option>'; 
         } 
        ?> 
       </select> 
      </td> 
      <td> 
       <label>Select Level : </label> 
      </td> 
      <td> 
       <select id="level"> 
         <option value="">Please Select</option> 
       </select> 
      </td> 
     </tr> 
    </table></center> 
</form> 
</body> 
</html> 

level.php

<?php 
//connect to the database 
$con = mysql_connect("localhost","root","12345") or die("error ".mysql_error()); 

//connect to the trav table 
mysql_select_db("trav",$con) or die("error ".mysql_error()); 

$building = mysql_real_escape_string($_GET['parent']); 

$query = "select * from "; 
$query = $query . $building; 
$query = $query . ";"; 
$result = mysql_query($query) or die('Error in Child Table!'); 

echo '<option value="">Please Select</option>'; 
while($row = mysql_fetch_assoc($result)) 
{ 
    echo '<option value="'. $row['lvl'] .'">'.$row['lvl'].'</option>'; 
} 
?> 

注意上面的代碼已經從jogesh的博客中。上面已經提到了這個鏈接。

希望它可以幫助像我這樣的PHP新手但是喜歡嘗試我們不同的事情的其他PHP程序員。