[英]Dropdown automatically selected a value
i need help for this problem. 我需要这个问题的帮助。 I want to make a dynamically dropdown and when i select a value from one dropdown to "A", the another dropdown will be set to "B".
我想进行一个动态下拉菜单,当我从一个下拉菜单中选择一个值到“ A”时,另一个下拉菜单将被设置为“ B”。
I have a javascript function for dynamically dropdown like this. 我有一个javascript函数,可以像这样动态下拉。
<script type="text/javascript">
function coba(){
document.getElementById("add").innerHTML +=
" <inputclass='department_name' type='text'
size='50' />";
}
</script>
REFERENCE: how to dynamically change item of two related combobox 参考: 如何动态更改两个相关组合框的项目
In Short: 简而言之:
file1.php
, Retrieve mysql tbl1
and display it in a combo box A . file1.php
,检索mysql tbl1
并将其显示在组合框A中 。 file2.php
via ajax and Display the output in file1.php
which is produced by file2.php
. file2.php
通过ajax将其传递给php文件file2.php
,并在file1.php
显示输出,该输出由file2.php
。 file2.php
, Retrieve mysql tbl2
with the Id passed by Ajax and generate a combo box B . file2.php
,使用Ajax传递的ID检索mysql tbl2
并生成一个组合框B。 Example: 例:
index.php index.php
<script type="text/javascript">
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
function ajax_function(url, postData, id)
{
xmlhttp=GetXmlHttpObject();
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", postData.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
document.getElementById(id).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.send(postData);
}
function dispSecond(Id)
{
var params = 'Id=' + Id ;
var DivId = 'dispDiv';
ajax_function('ajax_display.php', params, DivId);
}
</script>
<?php
/* Mysqli query to retrieve and store in $ArrayList(Id=>Text)
Example: $ArrayList = array(1=>'Ford',2=>'Chevy');
*/
?>
<select id="drop_first" name="drop_first" onchange="return dispSecond(this.value);">
<option value="0">[Select]</option>
<?php
foreach ($ArrayList as $k=>$v)
{
echo '<option value="'.$k.'">'.$v.'</option>';
}
?>
</select>
<div id="dispDiv"></div>
ajax_display.php ajax_display.php
<?php
$Id = isset($_REQUEST['Id']) ? $_REQUEST['Id'] : '';
if ($Id)
{
/* Mysqli query to retrieve and store in $SubArray where $Id
Example:
If $Id=1
$SubArray = array(1=>'Focus',2=>'Explorer');
If $Id=2
$SubArray = array(1=>'Cavalier',2=>'Impala', 3=>'Malibu');
*/
?>
<select id="drop_second" name="drop_second">
<option value="0">[Select]</option>
<?php
foreach ($SubArray as $k=>$v)
{
echo '<option value="'.$k.'">'.$v.'</option>';
}
?>
</select>
<?php
}
?>
Note: 注意:
Use Mysqli or PDO instead mysql 使用Mysqli或PDO代替mysql
Below Demo and Download are based on arrays, you can implement by using mysqli retrieval. 在“演示和下载”下面是基于数组的,您可以使用mysqli检索来实现。
Also You can try using $.ajax which is more easy also. 您也可以尝试使用$ .ajax,这也更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.