2015-10-07 71 views
1

我已经使用验证码的形式,如果我尝试点击提交(通过鼠标)甚至做出正确的验证码警告I'mm越来越之后“验证码错误”,而可悲的事情如果我按下输入键(通过键盘),即使提供错误的验证码后,它会显示“谢谢”文本。jQuery的PHP验证码验证

的index.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('');   
     } 
    }); 
    } 

Contact.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 = '[email protected]'; 
     $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; 
    } 
    ?> 

Get_Captcha.php

<?php 
    session_start(); 
    $string = ''; 
    for ($i = 0; $i < 5; $i++) { 
     $string .= chr(rand(97, 122)); 
    } 

    $_SESSION['random_number'] = $string; 
    $dir = 'fonts/'; 
    $image = imagecreatetruecolor(165, 50); 
    $num = rand(1,2); 
    if($num==1) 
    { 
     $font = "Capture it 2.ttf"; 
    } 
    else 
    { 
     $font = "Molot.otf"; 
    } 
    $num2 = rand(1,2); 
    if($num2==1) 
    { 
     $color = imagecolorallocate($image, 113, 193, 217); 
    } 
    else 
    { 
     $color = imagecolorallocate($image, 163, 197, 82); 
    } 

    $white = imagecolorallocate($image, 255, 255, 255); 
    imagefilledrectangle($image,0,0,399,99,$white); 
    imagettftext ($image, 30, 0, 10, 40, $color, $dir.$font,   $_SESSION['random_number']); 
    header("Content-type: image/png"); 
    imagepng($image); 
    ?> 

回答

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

0

我没试过这个代码,只需用我的逻辑wtighting 我认为它的更好,如果你正在使用一个单独的页面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']; 
} 
?>