我想填充基于从数据库填充的动态Dropbox的文本框。基于动态下拉框填充文本框
我的代码如下:
的index.php
<?php
include "../conn.php";
?>
<html>
<head>
<title>Changing textbox value based on dropdown list using Ajax and PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
function getXMLHTTP() {
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getCurrencyCode(strURL){
var req = getXMLHTTP();
if (req){
//function to be called when state is changed
req.onreadystatechange = function(){
//when state is completed i.e 4
if (req.readyState == 4) {
// only if http status is "OK"
if (req.status == 200){
document.getElementById('cur_code').value=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
</head>
<body style="font: 12px Verdana, Arial, Helvetica, sans-serif;">
<form style="text-align:center" method="post" action="" name="form1">
<p style="color:#000099 ">When you change the dropdown list, the respective currency code of the country will be displayed in the textbox which is fetched from PHP using Ajax. </p>
<p>Department : <?PHP
echo "<select name= 'Department' class='form-control selectpicker' onChange='getCurrencyCode('find_ccode.php?country='+this.value)' Required>";
echo '<option value="">'.'--Please Select Department--'.'</option>';
$sql = "SELECT ID,Name FROM Departments";
$query = sqlsrv_query($conn,$sql);
$query_display = sqlsrv_query($conn,$sql);
while($row=sqlsrv_fetch_array($query_display,SQLSRV_FETCH_ASSOC)){
echo "<option value='". $row['Name']."'>".$row['Name']. '</option>';
}
echo "</select>";
?>
ID : <input type="text" name="cur_code" id="cur_code" ></p>
</form>
</body>
</html>
find_ccode.php
<?php
$country=$_REQUEST['country'];
include '../conn.php';
$sql = "SELECT ID,Name FROM Departments Name='$country'";
$fetched=sqlsrv_query($conn,$sql) ;
if($fetched === false) { die(print_r(sqlsrv_errors(), true));}
while($sno=sqlsrv_fetch_array($fetched,SQLSRV_FETCH_ASSOC))
{
echo $formno=$sno['ID'];
}
}
?>
我有什么
我想要什么:
是在下拉菜单选择向下应该在文本框中显示特定部门的ID号。我还附上了什么,我试图做
摘录但它似乎并没有工作。你认为我哪里出错了?感谢所有帮助:)
每个'
您需要使用JavaScript,才能在选择更改时设置#cur_code的值。我认为提出请求来获取每个ID是一个糟糕的主意,这只是一个数字,将其存储在选项的数据ID中。 –