在PHP中使用JavaScript自动填充
当另一个文本框字段有值时,我需要填充一个文本框。在JavaScript中,我面临一个问题,它无法完美工作 首先,让我们看看表单本身的html代码:在PHP中使用JavaScript自动填充,php,javascript,html,Php,Javascript,Html,当另一个文本框字段有值时,我需要填充一个文本框。在JavaScript中,我面临一个问题,它无法完美工作 首先,让我们看看表单本身的html代码: <div style="width: 145px; height: 30px; float: left; margin-top: 5px; margin-left: 3px;"> <label>Product Segment</label><label style="color: #F00;">
<div style="width: 145px; height: 30px; float: left; margin-top: 5px; margin-left: 3px;">
<label>Product Segment</label><label style="color: #F00;">*</label>
</div>
<div class="ui-widget"
style="width: 145px; height: 30px; float: left; margin-top: 5px; margin-left: 3px;">
<input type="text" name="productsegment" id="productsegment" onkeyup="getagentids();" value="<?php echo $productsegment;?>" />
</div>
<!--Row3 end-->
<!--Row3 -->
<div style="width: 145px; height: 30px; float: left; margin-top: 5px; margin-left: 3px;">
<label>Product Group</label><label style="color: #F00;">*</label>
</div>
<div
style="width: 145px; height: 30px; float: left; margin-top: 5px; margin-left: 3px;">
<input type="text" name="productgroup" id="productgroup" value="<?php echo $productgroup;?>" />
</div>
产品板块*
请在productgroupautofill.php中尝试以下代码
require_once 'Mysql.php';
$dblink = new Mysql();
$dblink->__construct();
$param=$_GET['param'];
if(strlen($param)>0)
{
$result = mysql_query("select productgroup from productsegmentmaster where productgroup LIKE '$param%'");
if(mysql_num_rows($result)==1)
{
while($myrow = mysql_fetch_array($result))
{
$agentname = $myrow["productgroup"];
$textout .= $agentname;
}
}
else
{
$textout=" , , ,".$param;
}
}
echo $textout;
您将httpobject
定义为:
httpobject = GetHttpObject();
但是您尝试使用http
:
http.open( ... )
将您的定义更改为此,因为您在整个代码中使用http
:
http = GetHttpObject();
您可能还希望在全局范围中声明它:
var url = "productgroupautofill.php?param=";
var http;
不能在循环外使用tectout变量。尝试在循环中使用echo,您应该会看到一些结果,事实上,您根本不需要那个变量。只需回显$myrow[“productgroup”];如果条件为true,并将“,”$param分配给另一个变量,如果为false则回显Define将无法正常工作
。它将进入函数直到http.open状态…然后它将不工作…:(…错误消息为?无错误消息..它不再工作…:(我认为http.open连接未建立…找不到对象!此服务器上未找到请求的URL。上的链接似乎错误或过时。请将错误告知页面作者。如果您认为这是服务器错误,请联系。错误404
Apache/2.4.3(Win32)OpenSSL/1.0.1c PHP/5.4.7My query未执行…正确…:(PHP单独工作吗?尝试从浏览器调用它,而不是Javascript。当PHP工作时,继续调试ajax。感谢…回复…但它工作完美…我犯了一些错误…:P…你的错误是正确的…)productgroupautofill.php是否位于调用它的文件所在的同一目录中?在返回数组后,您还需要拆分该数组。如果有多个数组,您可以告诉javascript这些值是用“,”拆分的,它会将这些值放入数组中
http = GetHttpObject();
var url = "productgroupautofill.php?param=";
var http;
var url = "productgroupautofill.php?param=";
var httpobject;
function GetHttpObject() {
if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest)
return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
}
function getagentids() {
httpobject = GetHttpObject();
if (httpobject != null) {
var idValue = document.getElementById("productsegment").value;
var myRandom = parseInt(Math.random() * 99999999);
// cache buster
httpobject.open("GET", url + escape(idValue) + "&rand=" + myRandom, true); // from
// here
// it
// wont
// work
httpobject.onreadystatechange = handleHttpResponse;
httpobject.send(null);
}
}
function handleHttpResponse() {
if (httpobject.readyState == 4) {
results = httpobject.responseText;
alert(results);
document.getElementById('productgroup').value = results;
}
}