[英]How to select the second option based on the first option (both select option are from the database)
Hi guys I'm new to php and javascript... This is the database that link with each other...大家好,我是 php 和 javascript 的新手...这是相互链接的数据库...
Below are the code I use to call the first table下面是我用来调用第一个表的代码
<label for="negeri">Negeri</label>
<select name="kod_negeri" id="kod_negeri" class="form-control">
<option selected value="00">Sila Pilih Negeri</option>
<?php
require_once '../SBDB.php';
$sql_run = mysqli_query($link, "SELECT * FROM sbhb07");
if (mysqli_num_rows($sql_run) > 0) {
while ($row = mysqli_fetch_array($sql_run)) {
?>
<option value="<?php echo $row['SBHB07_02']; ?>"><?php echo $row['SBHB07_02']; ?>
<?php echo $row['SBHB07_03']; ?></option>
<?php
}
}
?>
</select>
Below are the select tag that will change according to the above code... It will call data from the second table... The link of this two table is SBHB07_02 and SBHB08_02下面是根据上面代码会改变的select标签...它会从第二张表中调用数据...这两张表的链接是SBHB07_02和SBHB08_02
<div class="col-lg-4 col-auto">
<label for="daerah">Daerah</label>
<select name="daerah" id="daerah" class="form-control">
<option selected value="00">Sila Pilih Daerah</option>
</select>
</div>
Below are the javascript that I try to do... I need help as I'm not fully understand Javascript and JQuery as I know this problem need both.下面是我尝试做的 javascript...我需要帮助,因为我不完全理解 Javascript 和 JQuery,因为我知道这个问题需要两者。
You are almost there guy.你快到了,伙计。 make sure jquery file is in your application;
确保 jquery 文件在您的应用程序中; insert this jquery file into seperate js file to connect your application to server-side php script everytime you need to get data from server-side script;
每次需要从服务器端脚本获取数据时,将此 jquery 文件插入单独的 js 文件,以将您的应用程序连接到服务器端 php 脚本;
function SendByAjax(qrydata,urllink){
$.ajax({
crossOrigin: true,
type: "POST",
url: urllink,
data: qrydata,
cache: false,
success: function(html){
res = html.split("|||");
html = res[0];
if (html == 'Get2ndOpts'){
$("#daerah").html(res[1]); //update second select with new options
}else{
//other things
}
},error: function(jqXHR, textStatus, errorThrown){
alert("Operation failed");
}
}); }
UPDATE YOUR FIRST SELECT TAG Include onChange in the first select tag;更新您的第一个选择标签 在第一个选择标签中包含 onChange;
<select name="kod_negeri" id="kod_negeri" class="form-control" onChange="SendByAjax('id='+this.value+'&dwat=get2ndOptVals','myprocess.php');">
Optional: At the end of the page, you can also load second select tag with first value in first select tag;可选:在页面的末尾,您还可以在第一个选择标签中加载带有第一个值的第二个选择标签; like this;
像这样;
var FirstSelVal = $('#kod_negeri').value();
SendByAjax('id='+FirstSelVal+'&dwat=get2ndOptVals','myprocess.php');
LASTLY CREATE YOUR myprocess.php File Create myprocess.php file or any php file you are using to process request (connected to database);最后创建 myprocess.php 文件创建 myprocess.php 文件或任何用于处理请求的 php 文件(连接到数据库);
//your database connection file
required(database.php);
if($_REQUEST["dwat"] == "get2ndOptVals"){
$qry = mysqli_query($link,"SELECT * FROM sbhb08 WHERE SBHB08_02 ='".mysqli_escape_string($link,$_REQUEST["id"])."'");
if(mysqli_num_rows($qry) > 0){
while ($rs = mysqli_fetch_array($qry)) {
$opts .= '<option value="'.$rs["SBHB08_03"].'">'.$rs["SBHB08_04"].'</option>';
}
echo 'Get2ndOpts|||'.$opts;
}
}
Explanation: This is what I use to process multiple javascript, ajax, php request and response.说明:这是我用来处理多个 javascript、ajax、php 请求和响应的。 You can update it to perform more operations, apart from select options stuff
除了选择选项之外,您还可以更新它以执行更多操作
First fetch your 2nd select, then use onchange function for your first select.首先获取您的第二个选择,然后对您的第一个选择使用 onchange 功能。 Then get the value of first select then make a condition.
然后获取 first select 的值然后创建一个条件。 Then store the value into the second select.
然后将该值存储到第二个选择中。 Just like the following:
就像下面这样:
var sec = document.getElementById('secondSelect');
document.getElementById('kod_negeri').onchange = function() {
var value1 = document.getElementById('kod_negeri').value;
if(value1 == 'condition'){
sec.value = 'the value';
}
}
The SQL query should be: SQL 查询应该是:
SELECT sbhb08.SBHB08_04
FROM sbhb07 INNER JOIN sbhb08 ON sbhb07.SBHB07_02 = sbhb08.SBHB08_02
WHERE sbhb08.SBHB08_02 = [value_selected_with_first_select];
You can retrieve the value selected with the following JavaScript code:您可以使用以下 JavaScript 代码检索所选值:
let selectedNegeri = document.querySelector('#kod_negeri').value;
document.querySelector('#kod_negeri').addEventListener('change', (event) => {
selectedNegeri = event.target.value;
});
Anyway you cannot directly pass variables from JavaScript to PHP (and vice-versa) therefore you will have to use one of the following approaches:无论如何,您不能直接将变量从 JavaScript 传递到 PHP (反之亦然),因此您必须使用以下方法之一:
<select>
and read the selected value from $_POST
in order to populate the second <select>
(this will trigger a page reload)<select>
之后提交表单并从$_POST
读取所选值以填充第二个<select>
(这将触发页面重新加载)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.