2012-08-30 57 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程序员。