我有组合框显示数据库中的值列表。我希望当我从组合框中选择值时,它应该显示隐藏的数据库中的相应值而不使用提交函数(onchange =" this.form.submit ();")组合框
<div class="col-lg-6" style="display:none" id="d" >
<select name="id" id="id" class="span2" style="width:150px;" onchange="this.form.submit();">
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "valet";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$age = array();
$sql = "SELECT * FROM `tbl_companies` ";
$result = mysqli_query($conn, $sql);
// echo "User name=" . $row["name"]. "<br>";
?>
<option value="">-select company-</option>
<?php if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$age[] = $row;
}
?>
<?php foreach($age as $key=>$row) {
echo "<option value='" . $row['id'] . "' " . (($_REQUEST['id'] == $row['id']) ? "selected": "") . ">" . $row["id"] . "</option>";
?>
<?php }
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
header('Location: webservices.php');
}
mysqli_close($conn);
?>
</select>
<form action="" method="post" >
<br><br>
<br>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "valet";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$age = array();
$sql = "SELECT company_name ,acess_code ,register_on FROM `tbl_companies` where id=".$_REQUEST['id'] ." ";
$result = mysqli_query($conn, $sql);
// echo "User name=" . $row["name"]. "<br>";
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$age[] = $row;
?>
<br><br><br>
<input type="text" id="company_name" value="<?php echo $row["company_name"] ?>" name="company_name" style="width: 460px;height: 50px;overflow: hidden;" placeholder="Company Name*">
<br>
<br><br><br>
<input type="text" id="acess_code" value="<?php echo $row["acess_code"] ?>" name="acess_code" style="width: 460px;height: 50px;overflow: hidden;" placeholder="Acess Code*">
<br><br><br><br>
<input type="text" id="register_on" value="<?php echo $row["register_on"] ?>" name="register_on" style="width: 460px;height: 50px;overflow: hidden;" placeholder="Register On*">
<br><br><br><br>
<button name="editcompany" id="editcompany" type="submit" style="border:0;width:100px;margin-left: 45px;" >
<img src="images/save.png" alt="">
</button>
<button type="submit" style="border:0;width:100px;margin-left: 75px;">
<img src="images/cancel.png" alt="">
</button>
<?php
}
}
mysqli_close($conn);
?>
</form>
</div>
答案 0 :(得分:0)
试试这段代码......这里我为你创建了一个例子
<script>
function getComboB(sel) {
var roomtype=document.getElementById("roomtype");
var value = sel.options[sel.selectedIndex].value;
checkin.action = "checkin.php?item_combo="+value+"&roomtype="+value+"";
checkin.submit();
}
</script>
<form>
<select name="roomtype" id="roomtype" style="width:150px;" onchange="getComboB(this)">
<option><--Select--> </option>
<?php
$query=mysql_query("SELECT * FROM roomtype order by id");
while($row=mysql_fetch_assoc($query))
{
$val2=$row['id'];
?>
<option value="<?=$val2;?>" <? if ($_GET['roomtype'] == $val2) { echo "selected='selected'"; }?> > <?=$row['roomtype'];?> </option>
<?php }?>
</form>
`