javascript - 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
我无法找到一种方法来填充变量以完成查询并使用正确的商店正确重新加载下拉列表,而无需重新加载页面并丢失已在表单中完成的其余输入。
任何帮助,将不胜感激。
解决方案
使用以下代码创建了一个名为 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>
推荐阅读
- emacs - 每 30 分钟导出 ical
- java - 在 URL 上编码 GET 参数
- flask - Flask SocketIO 自动重新加载不起作用(在代码更改/开发中)
- python - 如何在 Django RFW 中将不同的序列化程序组装到 Json 响应中?
- regex - PHPUnit Symfony4 创建和测试自定义验证器
- javascript - 一起导入 OSM 和 TileArcGISRest 时打开图层 (5.0.0) 错误
- javascript - 使用 img 过滤器覆盖录制视频流
- excel - 如何获取二维数组excel vba中选定行的值?
- r - 基于两列中值组合的列
- python - Django 管理添加用户返回服务器错误 (500)