0

当另一个文本框字段具有值时,我需要填充一个文本框。在 JavaScript 中我遇到了一个问题,它不能完美地工作。

首先,让我们看看表单本身的 html 代码:

<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>

脚本代码:

<script type="text/javascript"> 
var url = "productgroupautofill.php?param=";

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

        http.open("GET", url + escape(idValue) + "&rand=" + myRandom, true); // from
        // here
        // it
        // wont
        // work
        http.onreadystatechange = handleHttpResponse;
        http.send(null);

    }
}
function handleHttpResponse() {
    if (http.readyState == 4) {
        results = http.responseText;
        alert(results);
        document.getElementById('productgroup').value = results;
    }
}            
</script>

productgroupautofill.php 文件内容。

<?php
require_once 'Mysql.php';
$dblink = new Mysql();
$dblink->__construct();

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;
4

4 回答 4

1
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;
    }
}
于 2012-11-29T09:54:56.300 回答
0

请尝试在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;
于 2012-11-29T09:45:52.070 回答
0

你定义httpobject为:

httpobject = GetHttpObject();

但是然后您尝试使用http

http.open( ... )

将您的定义更改为此,因为您http在整个代码中使用:

http = GetHttpObject();

您可能还想在全局范围内声明它:

var url = "productgroupautofill.php?param=";
var http;
于 2012-11-29T09:49:54.307 回答
0

You cannot use your tectout variable outside of the loop. Try using echo inside your loop and you should see some results coming through, In fact you don't need that variable at all. Just echo $myrow["productgroup"]; if condition is true and assign ' , , , '$param to anothher variable and echo if false

于 2012-11-29T17:29:34.883 回答