PHP、AJAX无法基于dropdownlist显示值
我的代码是从中提取的,但是我修改了以更好地适合我,但它并没有按照它应该的方式工作 Afile.phpPHP、AJAX无法基于dropdownlist显示值,php,ajax,dynamic,drop-down-menu,Php,Ajax,Dynamic,Drop Down Menu,我的代码是从中提取的,但是我修改了以更好地适合我,但它并没有按照它应该的方式工作 Afile.php <head> <script> function showUser(str) { if (str == "") { document.getElementById("txtHint").innerHTML = ""; return; }
<head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "getDescription.php?q=" + str, true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<?php
$comCtrl = new company_controller();
$companyArray = $comCtrl->retrieveAllCompany();
foreach($companyArray as $com) {
echo "<option value ='".$com."' >".$com."</option>";
}//end foreach
?>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
<?php
require_once('dbManager.php');
$q = intval($_GET['q']);
$dbMgr = new dbManager();
$conn = $dbMgr->getDBConnection();
$query = "select company_address from company where company_name = '$q'";
$result = mysql_query($query);
echo "<table border='1'>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['company_address'] . "</td>";
echo "</tr>";
}
echo "</table>";
$dbMgr->closeDBConnection($conn);
?>
<?php
//require('/config/config.php');
require(dirname(__FILE__) . '\..\config\config.php');
class dbManager{
function getDBConnection(){
// start connection
$connect = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if (!$connect)
{
die( 'Could not connect: '.mysql_error() );
}
// Select the database
if(!mysql_select_db(DB_NAME))
{
die('The database - '. DB_NAME .' does not exist!');
}
return $connect;
}
function closeDBConnection($connect){
// close the connection
mysql_close($connect);
}
}
?>
函数showUser(str){
如果(str==“”){
document.getElementById(“txtHint”).innerHTML=“”;
回来
}
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”、“getDescription.php?q=“+str,true”);
xmlhttp.send();
}
我建议您使用mysqli_query()
或PDO::query()
这是mysql\u query()
使用:
$q = trim($_GET['q']);
“从公司名称='”的公司中选择公司地址“$q.”代码>可以这样写:“从公司名称为“$q”的公司中选择公司地址
;在示例站点中,它使用的是mysqli,而在这里,您使用的是mysql。顺便问一下,mysql被剥离了,它不安全。您是否没有得到任何输出或得到意外的输出?这会影响结果吗?我没有得到预期的输出。当我第一次运行这个页面时,我会看到一个所有公司的下拉列表,在这个DDL下面会有一个文本,上面写着“个人信息将在这里”。当我查看页面时,我成功地得到了这一点。但是,当我选择一个公司值时,该公司的地址应该显示在带有“此处将列出人员信息”的文本中,但是相反,出现了另一个DDL。但是dbconnection语法对于两者都不同,因此在混乱中,您可能做了一些错误的事情
$q = trim($_GET['q']);