2013-03-07 60 views
0

我期待在我的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); 
?> 

問題:我該怎麼做?

+0

,而不是innerHTML的使用價值:

document.getElementById("Zip_code").innerHTML=""; document.getElementById("Zip_code").innerHTML=xmlhttp.responseText; 

,導致。像 document.getElementById(「Zip_code」)。value = xmlhttp.responseText; – 2013-03-07 12:41:37

回答

0

你必須在這兩條線來改變選擇爲Zip_code

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

我改變了...仍然沒有工作function showZip(str) { if(str ==「」) { document.getElementById(「Zip_Code」)。innerHTML =「」; return; } if(window.XMLHttpRequest) {//代碼爲IE7 +,Firefox,Chrome,Opera,Safari xmlhttp = new XMLHttpRequest(); } else {//代碼爲IE6,IE5 xmlhttp = new ActiveXObject(「Microsoft.XMLHTTP」); } xmlhttp.onreadystatechange =函數() { 如果(xmlhttp.readyState == 4 && xmlhttp.status == 200){ 的document.getElementById( 「ZIP_CODE」)。的setAttribute( 「值」,xmlhttp.responseText ) } } xmlhttp.open(「GET」,「Getzip.php?q =」+ str,true); xmlhttp.send(); } – 2013-03-07 12:44:03

+0

好吧..我有它的工作..但它把國家領域與Zip_Code領域...我期待分開他們......謝謝 – 2013-03-07 13:03:13

+0

我改變了數組$美元[] =數組($狀態,$ Zip_Code); – 2013-03-07 13:04:20