Jquery PHP Captcha验证

时间:2015-10-07 08:34:34

标签: php jquery html post

我已经使用验证码进行表格,如果我尝试点击提交(通过鼠标),即使在给出正确的验证码后,我得到的警告是“错误的验证码”,并且可悲事情就是如果我按下输入(通过键盘),即使在给出错误的验证码之后它也会显示“谢谢”文本。

的index.php

@helper.repeat(userForm("emails"), min = 1) { emailField =>

    @helper.inputText(emailField)

}

Contact.php

    <img src="get_captcha.php" alt="" id="captcha" />
    <br clear="all" />
    <input name="code" class="smoothborder" type="text" id="code" required="required" onblur="checkcode(this.value)">
    <div id="wrong" class="error" style="display: none;">Wrong Captcha</div>
    </div>
    <img src="refresh.jpg" width="25" alt="" id="refresh" style="margin-top:66px;margin-left:10px;" />

    function checkcode(value){
        $('#wrong').hide();
        $.post( "contact.php",{ namevalue: "code" }, function( data ) {     
            var returndata = data;      
        if(returndata == value){            
        } else {            
            $('#wrong').show();
            $('#code').val('');         
        }  
    });
  }

Get_Captcha.php

    <?php
    ob_start();
    session_start();
    if(isset($_REQUEST['namevalue']) != ''){
        echo $_SESSION['random_number'];
    }else{
        $name =  $_POST['name'];
        $email =  $_POST['email'];
        $mobile =  $_POST['mobile_no'];
        $url =  $_POST['url'];
        $comment =  $_POST['comment'];
        $response =  $_POST['response'];
        $Chooseoption =  $_POST['ddlselectionvalue'];
        $to = 'thejasvi@1800xchange.com';   
        $from = $name . ' <' . $email . '>';

        $subject = 'Message from ' . $name; 
        $message = 'Select Option: ' . $Chooseoption . '<br/>
        Name: ' . $name . '<br/>
        Email: ' . $email . '<br/>
        Mobile No.: ' . $mobile . '<br/>    
        URL: ' . $url . '<br/>          
        Message: ' . nl2br($comment) . '<br/>
        Response: ' . $response . '<br/>';
        echo $result = sendmail($to, $subject, $message, $from);
        if ($result==1){
            $result = 'Thank you! We have received your message.';
            header('location:index.php?result='.$result);
            ob_flush();
            exit();
        }else{
            $result = 'Sorry, unexpected error. Please try again later';
            header('location:index.php?result='.$result);
            ob_flush();
            exit();
        } 
    }

    function sendmail($to, $subject, $message, $from) {
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
        $headers .= 'From: ' . $from . "\r\n";

        $result = mail($to,$subject,$message,$headers);
        if ($result) return 1;
        else return 0;
    }
   ?>

2 个答案:

答案 0 :(得分:1)

问题是你在模糊时检查你的验证码:

onblur="checkcode(this.value)"

意味着只有当光标从验证码字段移动到其他东西(如按钮)时才会执行代码。

您可以改为在表单的onsubit事件上应用如下:

<form ... onSubmit="return checkcode()" >

但在这种情况下,你的函数检查码也需要返回true或false以使其正常工作。

function checkcode(value){
        $('#wrong').hide();
        $.post( "contact.php",{ namevalue: "code" }, function( data ) {     
            var returndata = data;      
        if(returndata == value){    
            return true;        
        } else {            
            $('#wrong').show();
            $('#code').val('');  
            return false;       
        }  
    });

此处有关于表单验证的更多信息:HTML/Javascript: Simple form validation on submit

答案 1 :(得分:0)

我没有尝试过这段代码,只是简单地用我的逻辑思考 如果您使用一个单独的页面进行captcha检查

,我认为会更好
function checkcode(value)
{
    $('#wrong').hide();
    $.post( "captchaCheck.php",{ namevalue: "code" }, 
    function( data )
    {     
        var returndata = data;      
        if(returndata != value){  
            $('#wrong').show();
            $('#code').val('');           
        }
    });
}

captchaCheck.php

<?php 
ob_start();
session_start();
if(isset($_REQUEST['namevalue']) == $_SESSION['random_number']){
    echo $_SESSION['random_number'];
}
?>