如何在表单标签中包含依赖项下拉列表?

时间:2019-01-28 11:31:59

标签: javascript php ajax html5 forms

我有一个有效的从属下拉列表,当未将其放置在表单标签中时,该列表可以平稳运行。一旦将其包含在表单标签中,它就会停止工作。如何在表单标签中包含此代码?

我只是尝试在不使用任何表单标签的情况下运行代码。它工作得很好。要将值传递到下一页,我使用了表单标签,那就是当我收到空白响应时(下拉列表未加载)。

index.php

<?php
require_once("dbcontroller.php");
$query ="SELECT * FROM flat";
$results = mysqli_query($conn,$query); 
?>
<html>
<head>
<title></title>

<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
function floor(val) {
    $.ajax({
    type: "POST",
    url: "get_info.php",
    data:'floor='+val,
    success: function(data){
        $("#floor").html(data);
    }
    });
}

function flatno(val1) {
    $.ajax({
    type: "POST",
    url: "get_info.php",
    data:'flatno='+val1,
    success: function(data){
        $("#flatno").html(data);
    }
    });
}


</script>
</head>
<body>


<form method="post" action="a.php"><br>


Select Wing:
<select name="wing" onChange="floor(this.value);">
<option value="">---------</option>
<?php
foreach($results as $row) {
?>
<option value="<?php echo $row["wing"]; ?>"><?php echo $row["wing"]; ?>                
</option>
<?php
}
?>
</select>

<br><br>
Select Floor:
<select name="floor" id="floor" onChange="flatno(this.value);">
<option value="">---------</option>
</select>


<br><br>
Select Flat No:
<select name="flatno" id="flatno">
<option value="">---------</option>
</select>

<input type="submit">

</form>

</body>
</html>    

get_info.php

<?php
require_once("dbcontroller.php");

if(!empty($_POST["floor"])) {
$query ="SELECT * FROM flat WHERE wing = '".$_POST["floor"]."'";
$results = mysqli_query($conn,$query); 
?>
    <option value="">Select Floor</option>
<?php
    foreach($results as $row) {
?>
    <option value="<?php echo $row["floor"]; ?>"><?php echo $row["floor"];         
?></option>
<?php
    }
}
?>

<?php
if(!empty($_POST["flatno"])) {
    $query ="SELECT * FROM flat WHERE floor = '".$_POST["flatno"]."'";
    $results = mysqli_query($conn,$query);
?>
    <option value="">Select Flat no</option>
<?php
    foreach($results as $row) {
?>
    <option value="<?php echo $row["flatno"]; ?>"><?php echo     
$row["flatno"]; ?></option>
<?php
    }
}
?>

a.php

<?php

include_once 'dbcontroller.php';

$wing =mysqli_real_escape_string($conn,$_POST['wing']);  
$floor =mysqli_real_escape_string($conn,$_POST['floor']);  
$flatno=mysqli_real_escape_string($conn,$_POST['flatno']); 

echo "<h2>Flat No : " . $flatno . "</h2>";
echo "<h2>Email : " . $floor . "</h2>";
echo "<h2>Email : " . $wing . "</h2>";
?>

我希望所有3个下拉列表都能正常工作,但只有第1个下拉列表加载,而其他2个为空。

1 个答案:

答案 0 :(得分:0)

您必须对函数使用不同的名称,例如floor1()和flatno1(),因为姓氏(floor()和flatno())用于另一个元素的ID名称,

以html代码显示:

将onchange =“ floor()”更改为onchange =“ floor1()

将onchange =“ flatno()”更改为onchange =“ flatno1()

使用javascript代码:

将“ 功能楼层()”更改为“ 功能楼层1()

将“ 功能flatno()”更改为“ 功能flatno1()