先在info數(shù)據(jù)庫(kù)中建立?test?數(shù)據(jù)表,內(nèi)容如下?然后建立頁(yè)面?名為04.html:<!DOCTYPE?html><meta?charset="utf-8"><html><head><title></title></head><body><form?action=""?method="post"><p>用戶名:?<input?type="text"?name="username"><span?id='reg'></span></p><p>email:?<input?type="text"?name="email"><span?id='rmail'></span></p><p><input?type="submit"?value="注冊(cè)"></p></form>?<script?type="text/javascript">var?user=document.getElementsByName('username')[0];var?reg=document.getElementById('reg');user.onblur=function(){var?url='04.php?un='+this.value;var?xhr?=?new?XMLHttpRequest();xhr.onreadystatechange?=?function(){if(this.readyState?==?4)?{if(this.responseText?==?0?){reg.innerHTML='<font?color="green">可用</font>';}else{reg.innerHTML='<font?color="red">不可用</font>';}}}xhr.open('get',url,true);xhr.send(null);}</script></body></html>?建立對(duì)應(yīng)PHP程序??名為?04.php<?php?header("Content-type:?text/html;?charset=utf-8");??if($con=mysql_connect('localhost','',''))?? {?//?echo?'連接服務(wù)器成功';?}else{echo?'連接失敗';}if(mysql_select_db('info'))?{//?echo?'連接數(shù)據(jù)庫(kù)成功';}else{echo?'鏈接數(shù)據(jù)庫(kù)失敗';}if(mysql_query('SET?NAMES?UTF8'))?{//?echo?'設(shè)置字符成功';?????????}else{mysql_error();}??$un?=?$_GET['un'];$users?=?array('zhangsan'?,?'lisi'?,?'wangwu');//?print_r($users);echo??in_array($un,?$users)?1:0;??/*??屏蔽1??$query?=?mysql_query('SELECT?name?FROM?test');?????$data?=?array();????while($row=mysql_fetch_row($query))?{????$data[]=$row[0];???}???//?print_r($data);?echo?in_array($un,?$data)?1:0;*/??>問(wèn)題來(lái)了:如果先把?屏蔽1的內(nèi)容注釋掉,?功能?是正常的。?在輸入框輸入?zhangsan?,?lisi?都會(huì)在后面顯示??“不可用”輸入其他內(nèi)部會(huì) ?顯示 “可用”我用print_r($users);?查詢內(nèi)容如下Array?(?[0]?=>?zhangsan?[1]?=>?lisi?[2]?=>?wangwu?)?然后,?我把$users?=?array('zhangsan'?,?'lisi'?,?'wangwu');print_r($users);echo??in_array($un,?$users)?1:0;?這一段全屏蔽掉,?使用?上面的?屏蔽1?的語(yǔ)句?先打印print_r($data);?內(nèi)容如下Array?(?[0]?=>?蘋(píng)果?[1]?=>?香蕉?[2]?=>?荔枝?[3]?=>?abc?)然后屏蔽print_r($data);??執(zhí)行??echo?in_array($un,?$data)?1:0;不管在?用戶名??欄輸入什么內(nèi)容,后面都顯示??“不可用”?為什么??我哪里寫(xiě)錯(cuò)了
mysql數(shù)據(jù)庫(kù)中的數(shù)據(jù)能正常調(diào)用出來(lái),但是ajax不能正常工作
rainy_li3676598
2017-06-10 15:43:16