由mysql php填充的链接下拉菜单

时间:2012-05-16 09:28:02

标签: php mysql drop-down-menu

打扰一下,如果我对这个术语有误,因为我是开发新手,但是我有一个链接的多个下拉菜单,以便上一个菜单的结果决定下一个菜单将填充下一个菜单。

即。如果我选择英国,下一个列表将显示,伦敦,如果我选择西班牙,下一个列表将显示马德里

我的下拉菜单是由mysql填充的,这很好但是如何在不调用其他页面的情况下将它们链接起来

以下是我所拥有的HTML的示例:

<div id="container">
  <h3>Price List</h3>
  <form method="post" action="" name="form1">
  <? 
  include 'classes.php';
  $query="SELECT * FROM Sex_Table";
  $result=mysql_query($query);
  ?>
  <div id="select-1">
    <select class="calculate" name="sdf" onChange="getClothing(this.value)">
    <option value="0">Please Select</option>
    <? while($row=mysql_fetch_array($result)) { ?>
    <option value=<?=$row['Price']?> sex_price="<?=$row['Price']?>"><?=$row['Sex_Name']?>&nbsp;(£<?=$row['Price']?>)</option>
  <? } ?>
    </select>
  </div>
  <? 
  $Sex=intval($_GET['Sex_var']);
  include 'classes.php';
  $query="SELECT * FROM Clothing_Table WHERE Sex_Table_ID='$Sex'";
  $result=mysql_query($query);
  ?>
  <div id="select-2">
    <select class="calculate" name="sdf" onChange="">
    <option value="0">Please Select</option>
    <? while($row=mysql_fetch_array($result)) { ?>
    <option value=<?=$row['Price']?> sex_price="<?=$row['Price']?>"><?=$row['Clothing_Name']?>&nbsp;(£<?=$row['Price']?>)</option>
  <? } ?>
    </select>
  </div>
</form>

您会注意到在更改时调用“getClothing(this.value)”

这是js:

function getClothing(Sex_Table_ID) {        

    $.ajax({
       type: "get",
       url: "findClothing.php",
       data: { Sex_var: Sex_Table_ID },
       error: function(error) { alert('System error, please try again.'); },
       success: function(data){ //data is the response from the called file
         $("#select-2").html(data); //this changes the contents of the div to data that was returned
       }
     });    
}

我需要它以便它不会调用url:“findClothing.php”,因为一旦这样做,我的价格就不再计算了。

我确定我真的不需要展示这部分但只是因为这是我的价格:

$(function(){
    $('.calculate').change(function() {
        var total = 0;
        $('.calculate').each(function() {
            if($(this).val() != 0) {
                total += parseFloat($(this).val());
            }
        });
        $('#total').text('£' + total.toFixed(2));
    });

});//]]>

1 个答案:

答案 0 :(得分:0)

您确定您的ajax数据包含价格为值的选项标记吗?

同样改变:

$('.calculate').change(function() {

$(".calculate").live("change", function(){