先在info數據庫中建立?test?數據表,內容如下?然后建立頁面?名為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="注冊"></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>?建立對應PHP程序??名為?04.php<?php?header("Content-type:?text/html;?charset=utf-8");??if($con=mysql_connect('localhost','',''))?? {?//?echo?'連接服務器成功';?}else{echo?'連接失敗';}if(mysql_select_db('info'))?{//?echo?'連接數據庫成功';}else{echo?'鏈接數據庫失敗';}if(mysql_query('SET?NAMES?UTF8'))?{//?echo?'設置字符成功';?????????}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;*/??>問題來了:如果先把?屏蔽1的內容注釋掉,?功能?是正常的。?在輸入框輸入?zhangsan?,?lisi?都會在后面顯示??“不可用”輸入其他內部會 ?顯示 “可用”我用print_r($users);?查詢內容如下Array?(?[0]?=>?zhangsan?[1]?=>?lisi?[2]?=>?wangwu?)?然后,?我把$users?=?array('zhangsan'?,?'lisi'?,?'wangwu');print_r($users);echo??in_array($un,?$users)?1:0;?這一段全屏蔽掉,?使用?上面的?屏蔽1?的語句?先打印print_r($data);?內容如下Array?(?[0]?=>?蘋果?[1]?=>?香蕉?[2]?=>?荔枝?[3]?=>?abc?)然后屏蔽print_r($data);??執行??echo?in_array($un,?$data)?1:0;不管在?用戶名??欄輸入什么內容,后面都顯示??“不可用”?為什么??我哪里寫錯了
mysql數據庫中的數據能正常調用出來,但是ajax不能正常工作
rainy_li3676598
2017-06-10 15:43:16