我已经发布了下面的代码。 有两个文件..
- getvendorname.php
- sales.htm(包含 JavaScript 函数)
我所做的是,当模糊时,我需要在不重新加载页面的情况下从 getvendorname.php 检索值,并在不同的文本框中显示 vendor 名称和 id。
在这里,我必须使用 xmlhttprequest 对象将值存储到从数据库检索的不同文本框中。这里检索值,但我无法存储到不同的文本框中。它可以工作,但显示如下 o/p:201anne.我需要在不同的文本框中显示 vendor ID 和 vendor 名称。请帮助我
销售.htm
<script type='text/javascript'>
function getname()
{
var vendorID = document.getElementById("idvid").value;
var xmlhttp;
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("iddiv1").innerHTML=xmlhttp.responseText;
document.getElementById("iddiv2").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getvendorname.php?vendorid="+vendorID,true);
xmlhttp.send();
}
</script>
<html>
<head><title>vendor info</title> </head>
<body>
<td>Vendor Primary ID:</td>
<td><input type="text" id="idvid" name="vendor_primary_number" onblur="getname()">
</td>
<td> <div id="iddiv1"> </div></td>
<td> <div id="iddiv2"> </div></td>
</body>
</html>
获取 vendor 名称.php
<?php
$vid = $_GET['vendorid'];
$connection = mysql_connect('localhost','root','root');
mysql_select_db('bgm_score', $connection);
$r ="select vendorid,vendorname from vendor_info where vendorid ='$vid'";
$result = mysql_query($r, $connection);
$row = mysql_fetch_assoc($result);
echo $row["vendorid"];
echo $row["vendorname"];
?>
最佳答案
我对您的代码进行了一些更正以使其正常工作。首先,替换这一行:
document.getElementById("iddiv1").innerHTML=xmlhttp.responseText;
与:
document.getElementById("iddiv1").innerHTML=vendorID;
原因是您不需要从数据库中检索 vendor ID,因为这就是您要发送的数据。只需使用您拥有的变量即可。这也意味着您需要更改 PHP 代码。
从查询中删除vendorid字段:
$r ="select vendorid,vendorname from vendor_info where vendorid ='$vid'";
现在看起来像这样:
$r ="select vendorname from vendor_info where vendorid ='$vid'";
同时删除此行,因为不需要它:
echo $row["vendorid"];
关于javascript - 使用 xmlhttprequest 对象在两个文本框中显示值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37392830/