使用Mysql查询结果填充表单字段

时间:2013-05-12 13:48:28

标签: php ajax

我正在尝试使用sql查询的结果填充表单字段,但收效甚微。我确信它可能很简单,但我无法得到它。

任何有关解决此问题的帮助都表示赞赏

这两个文件的代码在

之下
**<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

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

<form>
<input name="pcode" onchange="showUser(this.value)">

<input name="state" id="state" value="<?php echo $_GET["state"]; ?>"/>

</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>**

getpcode.php

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

$con = mysql_connect('BusContacts.db.10922105.hostedresource.com', 'BusContacts', 'Macuni03!');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("BusContacts", $con);

$sql="SELECT Postcodes.locality,state,pcode FROM Postcodes WHERE Postcodes.pcode like '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Locality</th>
<th>State</th>
<th>PostCode</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['locality'] . "</td>";
  echo "<td>" . $row['state'] . "</td>";
  echo "<td>" . $row['pcode'] . "</td>";
  echo "</tr>";

  }
echo "</table>";

mysql_close($con);
?>

0 个答案:

没有答案