我期待在我的ajax調用上填寫State和Zip_code值。而不是在txtzip中。我想填入值,國家和ZIP_CODE領域從txtzip在ajax調用上填寫的文本字段
這裏是我的嘗試至今:
<input type="text" name="State" id="State" placeholder="NY" value="" size="5"/>
<input type="text" name="Zip_Code" placeholder="zip code" value="" size="10" />
<div id="txtZip"><b>Person info will be listed here.</b></div>
Ajax代碼
function showZip(str)
{
if (str=="")
{
document.getElementById("txtZip").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("txtZip").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","Getzip.php?q="+str,true);
xmlhttp.send();
}
PHP代碼
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', 'root');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Leadbook", $con);
$sql="SELECT * FROM Zip WHERE City = '".$q."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$State = $row['State'];
$Zip_Code = $row['Zip Code'];
$info[] = array('State' => $State, 'Zip Code' => $Zip_Code);
}
echo json_encode($info);
mysql_close($con);
?>
問題:我該怎麼做?
,而不是innerHTML的使用價值:
,導致。像 document.getElementById(「Zip_code」)。value = xmlhttp.responseText; – 2013-03-07 12:41:37