PHP,AJAX从HTML中自动填充输入字段选择?

时间:2012-12-09 00:49:50

标签: php javascript ajax

我一直试图找出一种基于初始文本从数据库中提取信息的正确方法 在html下拉菜单中选择。

这是我的代码:

<html>
<head>
</head>
<script src="testjs.js"></script>
<?php
    $host = "";
    $username = ""; 
    $password = "";
    $database = "";
    mysql_connect($host, $username, $password);
    mysql_select_db($database);
?>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<?php
    $Query = mysql_query("SELECT * FROM population");
    while ($Rows = mysql_fetch_array($Query))
    {
        $ID = $Rows['ID'];
        $Pop = $Rows['Pop'];
        $UniqueID = $Rows['uid'];
        echo "<option value=\"$UniqueID\">$Pop</option>";
    }
?>
</select>
</form>
<br>
<p>DB ID <input type="text" id="ids" name="ID" ></p>
<p>Population <input type="text" id="content" name="contet" ></p>
<p>Unique ID <input type="text" id="uid" name="uid" ></p>
<div id="GetInformation"><b>Person info will be listed here.</b></div>

</body>
</html> 

test.js包含:

function showUser(str)
{
if (str=="")
  {
  document.getElementById("GetInformation").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("GetInformation").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}

获取用户包含:

<?php
$q=$_GET["q"];

$con = mysql_connect('', '', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("DropDown", $con);

$sql="SELECT * FROM population WHERE uid = '".$q."'";

$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
  {
    $ID = $row['ID'];
    $Pop = $row['Pop'];
    $UID = $row['uid'];
?>
<script type="text/javascript">
var ids = '<?php echo json_encode($ID); ?>';
var content = '<?php echo json_encode($Pop); ?>';
var uid = '<?php echo json_encode($UID); ?>';
</script>
 <?php }
mysql_close($con);
?> 

1 个答案:

答案 0 :(得分:1)

尝试更改

while($row = mysql_fetch_array($result))
  {
    $ID = $row['ID'];
    $Pop = $row['Pop'];
    $UID = $row['uid'];
?>
<script type="text/javascript">
var ids = '<?php echo json_encode($ID); ?>';
var content = '<?php echo json_encode($Pop); ?>';
var uid = '<?php echo json_encode($UID); ?>';
</script>
 <?php }

while($row = mysql_fetch_array($result))
{
    $ID = $row['ID'];
    $Pop = $row['Pop'];
    $UID = $row['uid'];
    echo $ID . ' - ' . $Pop . ' - ' . $UID;
}

这应该有效。但是有更好的方法,因为它们会在您的客户端为您提供更多访问权限。例如发送一个JSON对象,一个简单的例子是:

$info = array();
while($row = mysql_fetch_array($result))
{
    $ID = $row['ID'];
    $Pop = $row['Pop'];
    $UID = $row['uid'];

    $info[] = array( 'id' => $ID, 'pop' => $Pop, 'uid' => $UID );
}
echo json_encode($info);

你的JS就像:

if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
    var data = JSON.parse(xmlhttp.responseText);
    for(var i=0;i<data.length;i++) 
    {
      document.getElementById("GetInformation").innerHTML += data[i].id + ' - ' + data[i].pop + ' - ' + data[i].uid;
    }
}

注意:如果您使用的浏览器不包含JSON库,则需要加载http://www.json.org/js.html。如果你想使用jQuery

,你的AJAX / DOM更改也会变得简单得多