【问题标题】:select box based on another select box value php基于另一个选择框值php的选择框
【发布时间】:2016-07-27 04:25:56
【问题描述】:

我使用 ajax 来做我的选择框,但是当我选择第一个选择框时,第二个选择框没有显示任何值。如何在第二个选择框中显示值?

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();
                                }
}
              ?>

【问题讨论】:

  • 任何控制台错误?
  • 在你的 ajax 中,尝试:data: {"brand":brand},
  • @Rasclatt 没有错误显示。
  • @Rasclatt 我已经更改了 ajax,它也没有显示。

标签: javascript php drop-down-menu


【解决方案1】:

在您的 ajax_city.php 页面上使用方括号而不是圆括号。 它应该是 -> !empty($_POST["brand"]))

希望它会起作用

【讨论】:

  • 我试过换支架了,还是不行。
  • 您在 ajax_city.php 上错过了一个右括号,添加该右括号并再试一次
  • 嗨,我添加了右括号,但下拉框的值也没有出来。
  • 使用以下代码并检查一次,希望可以正常工作
  • 我已经粘贴了另一个答案,检查一下
【解决方案2】:

索引:

                                <!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>';
                }
            }   
            ?>

【讨论】:

  • 我可以这样写吗?我已经在另一个帖子上发布了我的答案
【解决方案3】:

使用这段代码,我已经添加了所有的 cmets,只需跟随那个 cmets 并检查它是否有效。它对我来说工作正常。

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>';
                }       
            ?>

【讨论】:

  • 感谢代码,但这适用于 oledb 数据库吗?因为我正在使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 2013-02-21
相关资源
最近更新 更多