2015-10-07 93 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']; 
} 
?>