PHP验证用户名!碰见怪事了

yuanshiren89 2009-11-24 09:52:59
function query(){
if (xmlHttp.readyState == 4 ){
if (xmlHttp.status==200)
{
if (xmlHttp.responseText=="true"){
document.getElementById("checkusername").innerText="该帐号已被占用";
//document.getElementById("checkusername").innerText=xmlHttp.responseText;
document.all.isadd.value="true";
}else {
document.getElementById("checkusername").innerText="该帐号可用";
document.all.isadd.value="false";
}
}
}
}
就算xmlHttp.responseText 返回的值是 true 上面的JS代码也不会执行 谁帮忙解决一下 疯了

完整的代码
<input type="text" name="username" class="inputtext" size="25" onkeyup="usernamecheck()">


var xmlHttp;
function createAJAX(){
try{
xmlHttp = new XMLHttpRequest();
} catch(e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP")
} catch(e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch(e){
alert("您的浏览器不支持AJAX!");
return;
}
}
}
}
function usernamecheck(){
str=document.userinfo.username.value;
var url="userquery.php?str="+str;
if(str.length!=0){
createAJAX();
xmlHttp.onreadystatechange=query;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}else{
document.getElementById("checkusername").innerText="";
}
}


function query(){
if (xmlHttp.readyState == 4 ){
if (xmlHttp.status==200)
{
if (xmlHttp.responseText=="true"){
document.getElementById("checkusername").innerText="该帐号已被占用";
//document.getElementById("checkusername").innerText=xmlHttp.responseText;
document.all.isadd.value="true";
}else {
document.getElementById("checkusername").innerText="该帐号可用";
document.all.isadd.value="false";
}
}
}
}




$conn=mysql_connect('localhost','root','root');
mysql_select_db('shop', $conn);
$str=$_GET["str"];
$sql="select * from userinfo where username='$str' ";
$result=mysql_query($sql);
if(mysql_num_rows($result)==0){
echo "false";
}else{
echo "true";
}


php的输出都正确定
...全文
319 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
foolbirdflyfirst 2009-11-25
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 yuanshiren89 的回复:]
返回值的长度是错的...我注册的那个页面编码GB2312
PHP code$conn=mysql_connect('localhost','root','root');mysql_select_db('shop',$conn);$str=$_GET["str"];$sql="select * from userinfo where username='$str'";$result=mysql_query($sql);if(mysql_num_rows($result)==0){echo"false";
}else{echo"true";
}
检查是这个 没有设编码

但是为什么我用iframe 提交内容它输出的内容是正确的 而JS返回的长度又是错的
[/Quote]
就这几行内容,即使是gb2312也没关系,因为你echo的东西是ascii码0-127区的字符,不论什么编码都兼容这个码区。
就怕你这个php文件的文件编码是utf-8编码,且有bom头,返回串中会多上3个字节(EF BB BF)。
alandy 2009-11-25
  • 打赏
  • 举报
回复
楼猪啊,以后编码问题自己多注意!感觉帮你DEBUG真累啊~~~
alandy 2009-11-25
  • 打赏
  • 举报
回复

mb_internal_encoding("gb2312"); // 转变gb2312编码
mb_http_output("HTML-ENTITIES");
ob_start('mb_output_handler');

$conn=mysql_connect('localhost','root','root');
mysql_select_db('shop', $conn);
//注意数据库编码集应为GBK
//mysql_query("SET NAMES 'GBK'");
$str=$_GET["str"];
$sql="select * from userinfo where username='$str' ";
$result=mysql_query($sql);
if(mysql_num_rows($result)==0){
echo "false";
}else{
echo "true";
}

dzxccsu 2009-11-25
  • 打赏
  • 举报
回复

header('Content-type:text/html;charset=gb2312');
$conn=mysql_connect('localhost','root','root');
mysql_select_db('shop', $conn);
$str=$_GET["str"];
$sql="select * from userinfo where username='$str' ";
$result=mysql_query($sql);
if(mysql_num_rows($result)==0){
echo "false";
}else{
echo "true";
}


yuanshiren89 2009-11-25
  • 打赏
  • 举报
回复
返回值的长度是错的...我注册的那个页面编码GB2312

$conn=mysql_connect('localhost','root','root');
mysql_select_db('shop', $conn);
$str=$_GET["str"];
$sql="select * from userinfo where username='$str' ";
$result=mysql_query($sql);
if(mysql_num_rows($result)==0){
echo "false";
}else{
echo "true";
}

检查是这个 没有设编码

但是为什么我用iframe 提交内容它输出的内容是正确的 而JS返回的长度又是错的
foolbirdflyfirst 2009-11-25
  • 打赏
  • 举报
回复
注意你php文件的文件编码,如果是utf-8+bom头就肯定错了
你先alert(xmlHttp.responseText.length)看下返回串长度,如果返回true,应为4.
还有一些js的书写要注意兼容问题,比如document.all在firefox下不支持,应该用标准的document.getElementById。。
yuanshiren89 2009-11-25
  • 打赏
  • 举报
回复
试了各位的方法 还是不行啊...返回值是正确的 我在php页面输出sql 语句及结果 都是我所查询的值
不知道怎么上传图片...要不然上传上来给各位看看
alandy 2009-11-25
  • 打赏
  • 举报
回复
楼主代码存在不少问题,这里给你重新写过~

<script type="text/javascript">
var xmlHttp;

function createAJAX()
{
xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

function usernamecheck(str) {
xmlHttp=createAJAX();
if (xmlHttp==null)
{
alert ("您的浏览器不支持AJAX!");
return;
}
var url="userquery.php?str="+str;
xmlHttp.onreadystatechange=query;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function query(){
if (xmlHttp.readyState == 4 ){
if (xmlHttp.status==200){
if (xmlHttp.responseText=="true"){
document.getElementById("checkusername").innerHTML="该帐号已被占用";
//document.getElementById("checkusername").innerText=xmlHttp.responseText;
document.all.isadd.value="true";
}else {
document.getElementById("checkusername").innerHTML="该帐号可用";
document.all.isadd.value="false";
}
}
}
}

</script>


HTML 表单如下:

<div>
<input type="text" name="username" class="inputtext" size="25" onblur="usernamecheck(this.value);">
<div>

<div id="checkusername"></div>

PHP代码很简单就不写了,用你上面代码就可以了~~~
有不明白的地方再跟贴说明,下班之前我会再关注下此贴。
task555 2009-11-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 phpboy005 的回复:]
引用 1 楼 task555 的回复:
似乎没问题,楼主好好检查一下

可以alert(xmlHttp.responseText)一下看看

另外因为缓存原因,如果使用ie的话每改一次要清空缓存或另开页面测试才能看到改过的效果。



PHP codeheader("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0",FALSE);header("Pragma: no-cache");/* 与HTTP1.0兼容*/
[/Quote]

这是一种解决方案,另一种就是url后再加个变化的参数比如时间
phpboy 2009-11-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 task555 的回复:]
似乎没问题,楼主好好检查一下

可以alert(xmlHttp.responseText)一下看看

另外因为缓存原因,如果使用ie的话每改一次要清空缓存或另开页面测试才能看到改过的效果。

[/Quote]


header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
header("Pragma: no-cache"); /* 与HTTP1.0兼容 */
海泪知己 2009-11-25
  • 打赏
  • 举报
回复
大体上是没有问题的,你找个js解释器运行看看,能提示什么地方出错的那种。
alandy 2009-11-25
  • 打赏
  • 举报
回复
楼主可以结贴了!等分中。。。
quansheng3510 2009-11-25
  • 打赏
  • 举报
回复
那就说明你的conn.php有错误了,仔细检查下,在页面中写跟用include效果应该是一样的。
foolbirdflyfirst 2009-11-25
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 yuanshiren89 的回复:]
刚自己找了下 发现问题在于
$conn=mysql_connect('localhost','root','root');
mysql_select_db('shop', $conn);
这两句我是用include("conn.php")这个引用的 只要引用就有问题 不引用就没事 是怎么一回事

[/Quote]
conn.php文件都有什么东西?这个文件的文件编码是什么编码?
还有你最好用firebug去看看ajax发送请求后返回串的具体内容,很方便的.
chenghelin 2009-11-25
  • 打赏
  • 举报
回复
看看
yuanshiren89 2009-11-25
  • 打赏
  • 举报
回复
刚自己找了下 发现问题在于
$conn=mysql_connect('localhost','root','root');
mysql_select_db('shop', $conn);
这两句我是用include("conn.php")这个引用的 只要引用就有问题 不引用就没事 是怎么一回事
阿_布 2009-11-24
  • 打赏
  • 举报
回复

function query(){
if (xmlHttp.readyState == 4 ){
if (xmlHttp.status==200)
{
if (decodeURIComponent(xmlHttp.responseText)=="true"){
document.getElementById("checkusername").innerText="该帐号已被占用";
//document.getElementById("checkusername").innerText=xmlHttp.responseText;
document.all.isadd.value="true";
}else {
document.getElementById("checkusername").innerText="该帐号可用";
document.all.isadd.value="false";
}
}
}
}
task555 2009-11-24
  • 打赏
  • 举报
回复
似乎没问题,楼主好好检查一下

可以alert(xmlHttp.responseText)一下看看

另外因为缓存原因,如果使用ie的话每改一次要清空缓存或另开页面测试才能看到改过的效果。

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧