index.php
<script type="text/javascript" src="ajax.js"></script><form name="myform" action="" method="post" enctype="text/plain"><input type="text" name="user" value="" onblur="funphp100('php100')"/>用户名:<div id="php100"></div></form>adjax.js
var xmlHttp;
function S_xmlhttprequest()
{
if(window.ActiveXobject)
{
xmlHttp=new ActiveXObject('Microsoft.XMLHttp');


}
else 
{
xmlHttp=new XMLHttpRequest();

}
}function funphp100(name){
var f=document.myform.user.value;
alert(f);
S_xmlhttprequest();
xmlHttp.open("GET","for.php?id="+f ,true);
xmlHttp.onreadystatechange=byphp;
xmlHttp.send(null);}
function byphp()
{
if(xmlHttp.readyState==1){
document.getElementById('php100').innerHTML="<img src=loading.jpg>";


}
if(xmlHttp.readyState=4){
if(xmlHttp.status==200){
var byphp100=xmlHttp.response.Text;
document.getElementById('php100').innerHTML=byphp100;

}

}}
for.php
<?php
if ($_GET[id])
{
sleep(1);
$conn=mysql_connect("localhost","root","123");
mysql_select_db("zuoye",$conn);
$sql="select * from'user'where name='$_GET[id]'";
$q=mysql_query($sql);
if(is_array(mysql_fetch_row($q))){
echo "用户名已经存在";

}
else {
echo "可以使用";

}
}
?>按照正常的思维来说他应该 显示出用户名已经存在 或者 可以使用  但是这里只提示没有undefined  

解决方案 »

  1.   

    js
    1.  byphp()方法中
    if(xmlHttp.readyState=4)   -> if(xmlHttp.readyState==4)
    2. 
    var byphp100=xmlHttp.response.Text; ->  responseTextphp :if ($_GET[id])   ->   if(isset($_GET['id']))   中id 加上引号,不加按正常情况会有一个警告的。
      

  2.   

    建议使用jquery操作ajax,比较简单
      

  3.   

    <?php
    echo "hello";
    if ($_GET['id'])
    {
    sleep(1);
    $conn=mysql_connect("localhost","root","123");
    mysql_select_db("zuoye",$conn);
    $sql="select * from'user'where name='$_GET[id]'";
    $q=mysql_query($sql);
    $num=mysql_num_rows($q);
    if($num>0){
    echo "用户名已经存在";

    }
    else {
    echo "可以使用";

    }
    }
    ?>
    修改了一下代码的确如你们所说跳转到了for.php  不幸的是还是遇到了错误   这个错误 我第一次见到 
    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\usr\www\html\ajax\a4\for.php on line 10
    ����ʹ�� 
      

  4.   

    本帖最后由 PhpNewnew 于 2012-01-18 17:45:13 编辑
      

  5.   

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\usr\www\html\ajax\a4\for.php on line 10
    意思是你的SQL语句错了,你的SQL语句真是神奇
    $sql="select * from'user'where name='$_GET[id]'";
    应该改成
    $sql="select * from 'user' where name={$_GET[id]}";或者
    $sql="select * from'user'where name='".$_GET[id]."';希望成功