我想根据第一个选择填充第二个选择表单。两者都从sql数据库中检索它们的元素 [英] I want to populate a second select form based on the first one selection. Both are retrieving their elements from an sql database
问题描述
这是我在这里的第一篇文章。
我比HTML或Java脚本中的初学者要少,我只是从一个月左右开始这样做。
所以,这是我的第一个项目。
我需要使用从SQL数据库检索的数据填充两个选择表单。
第一个选择是汽车品牌,第二个是基于第一个汽车模型。
我已经设法填充第一个选择并获取java脚本变量x中的选定值。
我应该如何继续下一步阅读所选品牌的型号?
以下是我迄今为止所做的工作:
<?php
include(sql.php); //连接数据库
$ sql_select_marca =select id,denumire from marca; // sql语句检索品牌
$ stmtMarca = sqlsrv_query($ conn,$ sql_select_marca);
?>
< html>
< title> Adauga Oferta< / title>
< head>
< link rel =stylesheettype =text / csshref =main.css>
< / head>
< body>
< form cars style =position:absolute>
< p>马卡:其中/ p为H.
form =carsstyle =position:absoluteonchange =marca()>
< option value =>选择品牌< / Seleteaza> < /选项>
<?php
while($ row = sqlsrv_fetch_array($ stmtMarca)){
echo'< option value ='。 $ row ['id']。 '>'。 $ row ['denumire']]。 < /选项>;
}
?>
< / select>
< p style =margin-left:290px>模型:其中/ p为H.
< option value =>选择模型< / Seleteaza> < /选项>
< / select>
< / form>
< p id =品牌>您已选择:< / p>
< script>
函数marca(){
var x = document.getElementById(SelectBrand)。value;
document.getElementById(Marca)。innerHTML =Ai selectat:+ x;
}
< / script>
< / body>
< / html>
第二种从数据库中选择应该是这样的:
select * from model where idmarca = $ x
*其中x是从第一个选择元素中读取的id
你需要用ajax来完成。在汽车品牌选择更改您需要进行ajax调用列出第二个选择框的值。
例如,请参阅此
This is my first post here. I'm less than beginner in HTML or Java script, I'm doing this only from a month or so. So, this is my first project. I need to populate two select forms with data retrieved from a SQL database. First select is a "Car brand" second one is "Car model" which is based on the first one. I have managed to populate the first select and to get the selected value in java script variable x. How should I proceed do next to read the models from the selected brand?
Here is what I have done until now:
<?php
include("sql.php"); //connection to the database
$sql_select_marca = "select id, denumire from marca"; //sql statement to retrieve brands
$stmtMarca = sqlsrv_query( $conn, $sql_select_marca);
?>
<html>
<title> Adauga Oferta </title>
<head>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<form cars style="position: absolute">
<p> Marca:</p>
<select id=SelectBrand class="select" name="marcaSelectata"
form="cars" style="position: absolute" onchange="marca()">
<option value="">Select brand</Seleteaza> </option>
<?php
while( $row = sqlsrv_fetch_array( $stmtMarca)) {
echo '<option value=' . $row['id'] . '>' . $row['denumire'] . '</option>';
}
?>
</select>
<p style="margin-left: 290px"> Model:</p>
<select id="SelectModel" class="select" name="model" form="cars" style="position: absolute;margin-left: 360px" onchange="model()" >
<option value="">Select model</Seleteaza> </option>
</select>
</form>
<p id="Brand"> You've selected:</p>
<script>
function marca() {
var x = document.getElementById("SelectBrand").value;
document.getElementById("Marca").innerHTML = "Ai selectat: " + x;
}
</script>
</body>
</html>
The second select from database should be something like:
select * from model where idmarca = $x
*where x is the id read from the first select
You need to do it with ajax. on Car brand select change you need to make a ajax call to list the second select box values. For Example refer this
这篇关于我想根据第一个选择填充第二个选择表单。两者都从sql数据库中检索它们的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!