Home >Backend Development >PHP Tutorial > php+Ajax进行验证用户名的时候遇到的编码有关问题

php+Ajax进行验证用户名的时候遇到的编码有关问题

WBOY
WBOYOriginal
2016-06-13 12:45:33727browse

php+Ajax进行验证用户名的时候遇到的编码问题
我在测试代码的时候,遇到了这个问题。
代码如下:
index.php

<br />
<script type="text/javascript" src="ajax.js"></script><br />
  <form name="myform" action="" method="post" enctype="text/plain"><br />
  用户名:<br />
  <input type="text" name="user" value="" onblur="funphp100('php100')"/><br />
  <div id="php100"></div><br />
  </form><br />

ajax.js
<br />
<br />
var xmlHttp;<br />
function S_xmlhttprequest() {<br />
	if(window.ActiveXObject) {<br />
		xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');<br />
	} else if(window.XMLHttpRequest) {<br />
		xmlHttp = new XMLHttpRequest();<br />
	}<br />
}<br />
<br />
function funphp100(name) {<br />
<br />
   var f=document.myform.user.value;<br />
	S_xmlhttprequest();<br />
	xmlHttp.open("GET","for.php?id="+f,true);<br />
	xmlHttp.onreadystatechange = byphp;<br />
	xmlHttp.send(null);<br />
}<br />
<br />
function byphp() {<br />
<br />
  	if(xmlHttp.readyState == 1) {<br />
		 document.getElementById('php100').innerHTML = "<img  src='loading.gif' alt=" php+Ajax进行验证用户名的时候遇到的编码有关问题 " >";<br />
	}<br />
<br />
    	if(xmlHttp.readyState == 4 ){<br />
		if(xmlHttp.status == 200) {<br />
          var byphp100 =  xmlHttp.responseText;<br />
          document.getElementById('php100').innerHTML = byphp100;<br />
		}<br />
	}<br />
<br />
<br />
}<br />

for.php
<br />
<?php<br />
if(@$_GET[id]){<br />
	sleep(1);<br />
 $conn=mysql_connect('localhost','root','');<br />
 mysql_select_db('test',$conn);<br />
<br />
 echo $sql="SELECT * FROM `user` where `user`='$_GET[id]'";<br />
 $q=mysql_query($sql);<br />
<br />
 if(is_array(mysql_fetch_row($q))){<br />
 	echo "<font color=red>用户名已经存在</font>";<br />
 }else<br />
 {<br />
   echo "<font color=green>可以使用</font>";<br />
 }<br />
}<br />
?>

我分别在本地环境和BAE环境都做了测试。
都出现了汉字不能正常的传送的现象,数据库中已经有“千手”这个用户了,但是还是提示可以使用该用户,貌似是编码的问题。
,求高人解答~ 

PHP Ajax MySQL 测试
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn