首页 > 解决方案 > PHP 驱动的选择函数

问题描述

我有一个默认启用的下拉菜单,并显示从数据库填充的选项。When an option is selected that is now blank it enables the drop down to the side if it.

echo '
<script>
   function check(){
      if(document.getElementById("company").value!="")
          document.getElementById("stores").disabled=false;
      else
          document.getElementById("stores").disabled=true;
                                }
 </script>

 <label class="form-control-label" for="input-last-name">Company</label>
 <select type="text" id="company" name="company" class="form-control form-control-alternative" onchange="check()">
    <option></option>';

    $sql = "SELECT * FROM companies WHERE CompanyID != '4'";
    $result = $con->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo '<option value='.$row['CompanyID'].'>'.$row['CompanyName'].'</option>';
        }
    }

    echo'
    </select>
    <label class="form-control-label" for="input-last-name">Store </label>
    <select id="stores" name="stores" class="form-control form-control-alternative" disabled>
        <option></option>';

        $sql = "SELECT * FROM stores";
        $result = $con->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()) {
                echo '<option value='.$row['storeid'].'>'.$row['storename'].'</option>';
            }
        }
        echo '
        </select>';

如您所见,从公司下拉列表中选择一个项目可以启用商店下拉列表。然而,目前它显示所有商店 - 不是分配给该公司的商店,SQL 需要

SELECT * FROM store  WHERE StoreID = $SelectedCompanyID

并不是

SELECT * FROM store

我无法找到一种方法来填充变量以完成查询并使用正确的商店正确重新加载下拉列表,而无需重新加载页面并丢失已在表单中完成的其余输入。

任何帮助,将不胜感激。

标签: javascriptphphtmlmysql

解决方案


使用以下代码创建了一个名为 fetch_data.php 的新页面

<?php
if(isset($_POST['get_option']))
{

include('includes/config.php');
$companies = $_POST['get_option'];


$sql = "SELECT * FROM stores WHERE companyid = '$companies'";
$result = $con->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo '<option value='.$row['storeid'].'>'.$row['storename'].'</option>';
    }
}
exit;
}
?>

将我的 HTML / PHP 更改为如下所示

echo '
<script>
   function check(){
      if(document.getElementById("company").value!="")
          document.getElementById("stores").disabled=false;
      else
          document.getElementById("stores").disabled=true;
                                }
 </script>

 <label class="form-control-label" for="input-last-name">Company</label>
 <select type="text" id="company" name="company" class="form-control form-control-alternative" onchange="check()">
    <option></option>';

    $sql = "SELECT * FROM companies WHERE CompanyID != '4'";
    $result = $con->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo '<option value='.$row['CompanyID'].'>'.$row['CompanyName'].'</option>';
        }
    }

    echo'
    </select>
    <label class="form-control-label" for="input-last-name">Store </label>
    <select id="stores" name="stores" class="form-control form-control-alternative">

    </select>';

然后终于在 JavaScript 中实现了它。

                           <script type="text/javascript">
                                function fetch_select(val)
                                {
                                $.ajax({
                                type: "post",
                                url: "fetch_data.php",
                                data: {
                                get_option:val
                                },
                                success: function (response) {
                                document.getElementById("stores").innerHTML=response; 
                                }
                                });
                                }

                            </script>


推荐阅读