在PHP中使用JavaScript自动填充

时间:2012-11-29 09:40:57

标签: 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;">*</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>

Jscript代码:

<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 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分: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;

答案 2 :(得分:0)

您将httpobject定义为:

httpobject = GetHttpObject();

但是您尝试使用http

http.open( ... )

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

http = GetHttpObject();

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

var url = "productgroupautofill.php?param=";
var http;

答案 3 :(得分:0)

你不能在循环之外使用你的tectout变量。尝试在循环中使用echo,你应该会看到一些结果,事实上你根本不需要那个变量。只需回显$ myrow [“productgroup”];如果condition为true,则将',,','$ param分配给另一个变量,如果为false则为echo