【问题标题】:Link dropdown menus populated by mysql php由 mysql php 填充的链接下拉菜单
【发布时间】:2012-05-16 17:36:43
【问题描述】:

对不起,如果我是开发新手,术语有误,但我链接了多个下拉菜单,以便上一个菜单的结果确定下一个菜单将填充下一个。

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

我的下拉菜单被 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));
    });

});//]]>

【问题讨论】:

  • 考虑在脚本开头只包含一次'classes.php'。在执行每个查询之前读取文件会给您的服务器带来不必要的负载,并且(取决于您在该文件中执行的操作)可能会不必要地打开与 mysql 服务器的新连接

标签: php mysql drop-down-menu


【解决方案1】:

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

也改变:

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

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

【讨论】:

  • 从 jQuery 1.7 开始,不推荐使用 .live() 方法。如果您使用更高版本的 jquery,请使用 .on() 附加事件处理程序。
猜你喜欢
  • 1970-01-01
  • 2010-10-06
  • 2013-11-27
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 2013-04-15
  • 1970-01-01
相关资源
最近更新 更多