用户注册界面使用ajax,处理ajax请求的是另一个php.
下面是注册界面的javascript部分:
<script type="text/javascript">
    function checkname(name)
    {
        var value=document.getElementById(name).value;
        var error_tip=name+"_error_tip";
        if(value=="")
        {
            document.getElementById(error_tip).innerHTML="Name cannot be empty";
            document.getElementById(error_tip).style.visibility="visible";
            return false;
        }
        else
        {
            var patt1=new RegExp("[0-9]");
            var patt2=new RegExp("[^A-z]");
            var r1=patt1.test(value);
            if(r1)
            {                
                document.getElementById(error_tip).innerHTML="Name can not contain digital";
                document.getElementById(error_tip).style.visibility="visible";
                return false;
            }
            var r2=patt2.test(value);   
            if(r2)
            {
                document.getElementById(error_tip).innerHTML="The name can not contain numbers or punctuation s";
                document.getElementById(error_tip).style.visibility="visible";
                return false;
            }
        }
        document.getElementById(error_tip).style.visibility="hidden";
        return true;
    }  
    function checkemail(name)
    {
        var validchar="1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-@.";
        var value=document.getElementById(name).value;
        var error_tip=name+"_error_tip";
        var flag1=0;
        var flag2=0;
        if(value=="")
        {            
            document.getElementById(error_tip).innerHTML="Email cannot be empty";
            document.getElementById(error_tip).style.visibility="visible";
            return false;
        }
        else
        {
            for(var i=0;i<value.length;i++)
            {
                if(validchar.search(value[i])==-1)
                {
                    document.getElementById(error_tip).innerHTML="Please enter a valid email";
                    document.getElementById(error_tip).style.visibility="visible";
                    return false;
                }
                if(value[i]=="@")
                {flag1++;}
                if(value[i]==".")
                {flag2++;}
            }
            if((flag1!=1)||(flag2!=1))
            {
                document.getElementById(error_tip).innerHTML="Please enter a valid email";
                document.getElementById(error_tip).style.visibility="visible";
                return false;
            }
            document.getElementById(error_tip).style.visibility="hidden";
            return true;
        }
    }   
    function checkpw(name)
    {
        var value=document.getElementById(name).value;
        var error_tip=name+"_error_tip";
        if(value=="")
        {
            document.getElementById(error_tip).innerHTML="Password cannot be empty";
            document.getElementById(error_tip).style.visibility="visible";
            return false;
        }
        else
        {
            document.getElementById(error_tip).style.visibility="hidden";
            return true;
        }
    }   
    function checkcpw(name)
    {
        var pw=document.getElementById('pw').value;
        var cpw=document.getElementById('cpw').value;
        var error_tip=name+"_error_tip";
        if(pw==cpw)
        {
            document.getElementById(error_tip).style.visibility="hidden";
            return true;
        }
        else
        {
            document.getElementById(error_tip).value="Two entered passwords do not match";
            document.getElementById(error_tip).style.visibility="visible";
            return false;
        }
    }  
    function getxmlhttp()
    {
        var xmlhttp;
        if(window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest()
        }
        else if(window.ActiveXObject)
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHttp");
        }
        return xmlhttp;
    }
    function getvalue(id)
    {
        if(id=="sub")
        {
            if(document.getElementById(id).checked) return 1;
            else return 0;
        }
        else
        return document.getElementById(id).value;
    }   
    function checkcode()
    {
        var code=document.getElementById("captcha_code").value;
        if(code.length<6) return false;
        else return true;
    }    
    function submitinfo()
    {
        var xmlhttp=getxmlhttp();
        if(checkname('fname')&&checkname('lname')&&checkpw('pw')&&checkemail('email')&&checkcpw('cpw')&&checkcode())
        {
            alert('submitinfo1');
            var info="fname="+getvalue('fname')+ "&lname="+getvalue('lname')+"&pw="+getvalue('pw')+"&email="+getvalue('email')+"&sub="+getvalue('sub')+
            "&captcha_code="+getvalue("captcha_code");
            xmlhttp.open("POST","register_action.php",true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.send(info);
            alert('submitinfo2');            
        }       
        xmlhttp.onreadystatechange=function()
        {
                if(xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    if(xmlhttp.responseText=="1")
                    {document.getElementById("code_result").innerHTML="the code entered was incorrect.";
                     document.getElementById("code_result").style.visibility="visible";}     
                    if(xmlhttp.responseText=="2")
                    {document.getElementById("reg_result").innerHTML="Create user successful.";
                     document.getElementById("reg_result").style.visibility="visible";}       
                    if(xmlhttp.responseText=="3")
                    {document.getElementById("reg_result").innerHTML="Email already exist";
                     document.getElementById("reg_result").style.visibility="visible";}      
                    if(xmlhttp.responseText=="4")
                    {document.getElementById("reg_result").innerHTML="Register success,send email failed";
                     document.getElementById("reg_result").style.visibility="visible";}      
                    if(xmlhttp.responseText=="5")
                    {document.getElementById("reg_result").innerHTML="Register user failed'";
       document.getElementById("reg_result").style.visibility="visible";}                                     
                }
        }     
    }
</script>

解决方案 »

  1. 这是界面的部分:
    <div style="margin:20px auto;text-align:center">
            <div  style="margin:10px auto;font-weight:bold;">Register user</div>
            <form id="userinfo" name="userinfo" method="POST" >
                <div id="reg_result" style="visibility: hidden;">Register successful</div>
                <div class="input_row">
                    <label for="email">Email</label>
                    <input type="text" size="30"  maxlength="30" name="email" id="email" onblur="checkemail('email')" />
                    <div class="error_tip" id="email_error_tip" style="visibility: hidden;"></div>
                </div>
                <div class="input_row">
                    <label for="fname">First name</label>
                    <input type="text" size="30" maxlength="20" name="fname" id="fname" onblur="checkname('fname')" />
                    <div class="error_tip" id="fname_error_tip" style="visibility: hidden;"></div>
                </div>
                <div class="input_row">
                    <label for="lname">Last name</label>
                    <input type="text" size="30" maxlength="20" name="lname" id="lname" onblur="checkname('lname')" />
                     <div class="error_tip" id="lname_error_tip" style="visibility: hidden;"></div>
                </div>
                <div class="input_row">
                    <label for="pw">Password</label>
                    <input type="password" size="30"  maxlength="10" name="pw" id="pw" onblur="checkpw('pw')" />
                    <div class="error_tip" id="pw_error_tip" style="visibility: hidden;"></div>
                </div>
                <div class="input_row">
                    <label for="cpw">Confirm Password</label>
                    <input type="password" size="30" maxlength="10" name="cpw" id="cpw" onblur="checkcpw('cpw')" />
                    <div class="error_tip" id="cpw_error_tip" style="visibility: hidden;"></div>
                </div>
                <div class="input_row">
                    <label for="sub">Subscribe</label>
                    <input type="checkbox" size="30" maxlength="10" name="sub" id="sub" />
                    <div class="error_tip" id="sub_error_tip" style="visibility: hidden;"></div>
                </div>
                <div style='clear:both'>
                    <div id='code_result' class='err_tip' style='visibility:hidden;width:500px;height:30px;line-height:30px'></div>
                    <div style='display:inline-block'>
                        <img id='captcha' src='/securimage/securimage_show.php' alt='CAPTCHA Image' />
                    </div>
                    <div style='display:inline-block'>
                        <a style='border-style: none;' href='#' title='Refresh Image' 
                        onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?sid=' + Math.random(); this.blur(); return false">
                        <img src='/securimage/images/refresh.png' height='32' width='32' alt='Reload Image' onclick='this.blur()' align='bottom' border='0' />
                        </a><br />
                        <strong>Entercode</strong><br />
                        <input type='text' id='captcha_code' name='captcha_code' size='12' maxlength='8' />
                    </div>
                </div>
                <div class="input_row" style="text-align: center;padding-top:30px">
                <input type="button" value='register' name="register" id="register" onclick="submitinfo()" />
                </div>
        </form>
    </div>
    这是处理ajax的register_action.php:
    <?php session_start(); ?>
    <?php 
        $rootdoc = $_SERVER['DOCUMENT_ROOT'];
        require_once $rootdoc.'/db./db.php';
        include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';
        require_once $_SERVER['DOCUMENT_ROOT'].'/PHPMailer_v5.1/class.phpmailer.php';    if(isset($_POST['captcha_code']))
        {
            $img = new Securimage();
            $img->image_bg_color = new Securimage_Color('#ffffff');
            $img->text_color = new Securimage_Color(rand(0, 64), rand(64, 128), rand(128, 255));
            $img->line_color = new Securimage_Color("#eaeaea");
            if ($img->check($_POST['captcha_code']) == false) 
            {
                $response="1";
                echo $response;
                die("Sorry, the code entered was incorrect");
              
            }
        }         if(isset($_REQUEST['fname'])&&isset($_REQUEST['pw'])&&isset($_REQUEST['lname'])&&isset($_REQUEST['email']))
        {
            $fname=$_REQUEST['fname'];      
            $lname=$_REQUEST['lname'];
            $pw=$_REQUEST['pw'];
            $email=$_REQUEST['email'];
            $sub_g=$_REQUEST['sub'];
            if($sub_g=="1")
            $sub="on";
            else
            $sub="off";
            /*
            if(isset($_REQUEST['sub']))
            $sub=$_REQUEST['sub'];
            else
            $sub="off";*/
            
            $database=new db("localhost","root","19801010_zxy","interview");        
            $rownum=$database->query_ifexist("users","email='$email'");
            //$npw=crypt($pw,"hsh");
            
            $uc_result=$database->query("","users","");
            if(mysql_num_rows($uc_result))
            $role=1;//admin
            else
            $role=0;//
            
            if($rownum==0)
            {
                $result=$database->insert("users","lname,fname,password,email,status,sub,role,created",
                                            "'$lname','$fname','$pw','$email','active','$sub',$role,now()");
                $id_result=$database->query("","users","email='$email'");
                $id_row=mysql_fetch_array($id_result);
                if($result)
                {                
                    $mail= new PHPMailer(); 
                    $address = $email;
                    $mail->IsSMTP(); 
                    $mail->CharSet='UTF-8';
                    $mail->Host = "smtp.qq.com"; 
                    $mail->SMTPAuth = true; 
                    $mail->Username = "2393590309@qq.com"; 
                    $mail->Password = "20110303_hsh"; 
                    $mail->From = "2393590309@qq.com";
                    $mail->FromName = "phpmailer";
                    $mail->AddAddress("$email", "");
                    //$mail->AddReplyTo("", "");
                    $mail->Subject = "PHPMailer,register info"; //Óʼþ±êÌâ
                    //$mail->Body = "http://127.0.0.9/activate.php/?id=".$row["id"];
                    //$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //¸½¼ÓÐÅÏ¢£¬¿ÉÒÔÊ¡ÂÔ
                    $mail->IsHTML(true);
                    $mail->Body = "
                    <html><head></head>
                    <body>welcome<br />
                    username:$email<br />
                    password:$pw
                    </body>
                    <html>";
                    if(!$mail->Send())
                    {
                    
                        $response="4";
         echo $response;
        die("");          
                    }
                    else
                    {         
                        $response="2";
                        echo $response;
                        die ("");
                    }
                }
                else
                {
                    $response="5";
               echo $response;
               die("");               
                }
            }
            else
            {
                $response="3";
                echo $response;
                die("get parameter error");
               
            }        
        }
    ?>
    javascript中的  alert('submitinfo1');和alert('submitinfo2');都有被alert,界面上看不到任何成功或失败的输出,打开firebug,在网络下看不到任何请求发出
      

  2. 确实好多~~~   用jquery 简单些 清楚些
      

  3. 先把验证那些注释掉,把ajax请求php调通再说。多半js某个地方错了阻止整个程序的运行。
      

  4.   $response="1";
      echo $response;
      die("Sorry, the code entered was incorrect");
    类似这种地方,请在echo语句后面截断,就难上面的代码来说,js获取到底返回数据是:1Sorry, the code entered was incorrect,那么你说能正确吗,如果要返回多个数据项,那么你可以josn一个数组返回,而不能想你上面写的那样
      


  5. if(checkname('fname')&&checkname('lname')&&checkpw('pw')&&checkemail('email')&&checkcpw('cpw')&&checkcode())其中的所有条件是否都为true?将每一项在此if之上alert出来看看
      

  6. 都是true,里面的alert有打印出来
      

  7. 在ajax的回调方法中,直接 alert php那边反回的值,若php那边有啥报错的,都可以显示错了!