简体   繁体   English

如何根据第一个选项选择第二个选项(两个选项都来自数据库)

[英]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 (反之亦然),因此您必须使用以下方法之一:

  1. You submit the form after the first <select> and read the selected value from $_POST in order to populate the second <select> (this will trigger a page reload)您在第一个<select>之后提交表单并从$_POST读取所选值以填充第二个<select> (这将触发页面重新加载)
  2. You create an AJAX request similarly to how it is explained here: https://www.w3schools.com/php/php_ajax_database.asp您创建的 AJAX 请求类似于此处的解释: https : //www.w3schools.com/php/php_ajax_database.asp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据第一个选择选项隐藏第二个选择选项 - hiding second select option based on first select option 如何获得第二个选择选项以基于第一个选择选项显示 - How do I get second select option to show up based on first select option jQuery如何根据“第三个选择列表”选项更改第二个选择列表,并根据第一个选择更改第二个选择列表 - How jQuery to change a second select list based on the Third select list option and second select based upon first 实现如何更改第二个 select 选项取决于第一个? - Materialize how to change second select option depended from the first? 3个选择元素,它们根据第一选择和第二选择中的所选选项更改选项 - 3 select elements that change options based on the selected option in the first and second select 动态下拉选择选项服务中的第一个选项,第二个选项将选择所选服务的医生。 - Dynamic dropdown select option first option from the service and the second option will select the doctor of the services selected. 在第二选择中获取空白选项取决于第一选择选项-AngularJS - Getting blank option in second select dependent on first select option - AngularJS 选择选项以基于mysql填充的下拉菜单更新第二个选择选项 - select option to update second select option based on mysql populated dropdowns 如何填充第二 <select>根据第一<select>选项 - How to populate second <select> according to first <select> option 显示基于第一个选择框的第二个选择框? - Show a second select box based on the option chosen in the first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM