Javascript PHP驱动的选择函数
我有一个默认启用的下拉列表,显示从数据库填充的选项。当选择一个现在为空的选项时,如果该选项为空,则将启用下拉列表Javascript PHP驱动的选择函数,javascript,php,html,mysql,Javascript,Php,Html,Mysql,我有一个默认启用的下拉列表,显示从数据库填充的选项。当选择一个现在为空的选项时,如果该选项为空,则将启用下拉列表 echo ' <script> function check(){ if(document.getElementById("company").value!="") document.getElementById("stores").disabled=false; else document.getEl
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>';
而不是
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>';
echo'
函数检查(){
if(document.getElementById(“公司”).value!=“”)
document.getElementById(“存储”).disabled=false;
其他的
document.getElementById(“存储”).disabled=true;
}
单位
';
$sql=“从CompanyID!=“4”的公司中选择*;
$result=$con->query($sql);
如果($result->num_rows>0){
//每行的输出数据
而($row=$result->fetch_assoc()){
回显“.$row['CompanyName']”;
}
}
回声'
商场
';
然后最终在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>
函数fetch_select(val)
{
$.ajax({
类型:“post”,
url:“fetch_data.php”,
数据:{
获取选项:val
},
成功:功能(响应){
document.getElementById(“stores”).innerHTML=响应;
}
});
}
使用下面的代码创建了一个名为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>';
echo'
函数检查(){
if(document.getElementById(“公司”).value!=“”)
document.getElementById(“存储”).disabled=false;
其他的
document.getElementById(“存储”).disabled=true;
}
单位
';
$sql=“从CompanyID!=“4”的公司中选择*;
$result=$con->query($sql);
如果($result->num_rows>0){
//每行的输出数据
而($row=$result->fetch_assoc()){
回显“.$row['CompanyName']”;
}
}
回声'
商场
';
然后最终在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>
函数fetch_select(val)
{
$.ajax({
类型:“post”,
url:“fetch_data.php”,
数据:{
获取选项:val
},
成功:功能(响应){
document.getElementById(“stores”).innerHTML=响应;
}
});
}
您可能需要使用,这样您就可以在不重新加载页面的情况下查询php脚本解决问题了吗?(参见最后一个db示例)您可能需要使用,这样您就可以在不重新加载页面的情况下查询php脚本。解决方案解决了您的问题吗?(见最后一个db示例)