<?php
include('config.php');
$isd=mysql_real_escape_string( $_GET['q'] );
$sql=mysql_query("SELECT zip FROM zipcod where abb='$isd' ") or ;
while( $row=mysql_fetch_array( $sql, MYSQL_ASSOC ) ){
$zip=$row['zip'];
echo '<option>'.$zip.'</option> ';
}
?>
这是ajax
<script>
function showUser(str) {
if (str=="") {
document.getElementById("zzips").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("zzips").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getstate.php?q="+str,true);
xmlhttp.send();
}
</script>
HTML
<select name="states"onchange="showUser(this.value)">
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AZ">AZ</option>
<option>AR</option>
<option>CA</option>
<option>CO</option>
<option>CT</option>
<option>DE</option>
<option>FL</option>
<option>GA</option>
<option>HI</option>
<option>ID</option>
<option>IL</option>
<option>IN</option>
<option>IA</option>
<option>KS</option>
<option>KY</option>
<option>LA</option>
<option>ME</option>
<option>MD</option>
<option>MA</option>
<option>MI</option>
<option>MN</option>
<option>MS</option>
<option>MS</option>
<option>MT</option>
<option>NE</option>
<option>NV</option>
<option>NH</option>
<option>NJ</option>
<option>NM</option>
<option>NY</option>
<option>NC</option>
<option>ND</option>
<option>OH</option>
<option>OK</option>
<option>OR</option>
<option>PA</option>
<option>RI</option>
<option>SC</option>
<option>SD</option>
<option>TN</option>
<option>TX</option>
<option>UT</option>
<option>VT</option>
<option>VA</option>
<option>WA</option>
<option>WV</option>
<option>WI</option>
<option>WY</option>
</select>
&#13;
此代码正在获取某些州的邮政编码,如CO,NJ,而非全部。有人可以让我知道错误,以便根据第一个下拉列表中选择的状态显示邮政编码
感谢您的帮助。
答案 0 :(得分:0)
$ sql = mysql_query(&#34; SELECT zip FROM zipcod,其中abb =&#39; $ isd&#39;&#34;)或;
您是否在代码中遗漏了某些内容,或者在此处再次输入时错过了它?也许你应该有像或者死的东西(&#34;错误信息&#34;); ?
答案 1 :(得分:0)
php中有一个小错误 - 您在or
语句后省略了该条件。
<?php
include('config.php');
$isd=trim( mysql_real_escape_string( $_GET['q'] ) ); /* or? */
/* Is the table actually called `zipcod` or `zipcode`? */
$sql=mysql_query( "SELECT `zip` FROM `zipcod` where trim( `abb` )='$isd';" ) or die('bad mojo');
while( $row=mysql_fetch_array( $sql, MYSQL_ASSOC ) ){
$zip=$row['zip'];
echo '<option>'.$zip.'</option> ';
}
mysql_free_result( $sql );
?>
帮助调试javascript / ajax请求:
function showUser(str) {
var dbg=true;
if( dbg )console.group('showUser');
if( str=="" ) {
document.getElementById("zzips").innerHTML="";
if( dbg )console.warn('Empty string');
return;
}
if( dbg )console.info( 'Initial value passed to ajax function: %s ',str );
xmlhttp=window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
if( dbg )console.info( 'XHR Object created: %s', xmlhttp );
xmlhttp.onreadystatechange=function() {
if( xmlhttp.readyState==4 && xmlhttp.status==200 ) {
document.getElementById("zzips").innerHTML=xmlhttp.responseText;
if( dbg )console.info('Response received: %s', xmlhttp.responseText )
}
}
xmlhttp.open( "GET", "getstate.php?q="+str, true );
if( dbg )console.info('Connection opened');
xmlhttp.send();
if( dbg )console.info('Request sent');
}