简体   繁体   中英

jQuery Using AJAX to validate Captcha (Securimage)

I read already a lot of Questions in here and tried several tutorials, but somehow it still won't work whatever I try to fix.

Here is my HTML Code (/login.php)

<form action="func/login.php" method="post" name="login">                      
  User: <input type="text" name="username" />
  Password: <input type="password" name="password" id="password"/>
  <img id="captcha" src="securimage/securimage_show.php" alt="CAPTCHA Image" />
  <input type="text" name="captcha_code" size="10" maxlength="6" />
  <a href="#" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false">[ Different Image ]</a>'
  <input type="submit" value="Login" /> 
</form>

and here my JS (/js/forms.js)

$(document).ready(function (){
  $( "form[name='login']" ).submit(function( event ) {      
    $.ajax({
      url: '../func/check_captcha.php',
      data: {cc: $( "input[name='captcha_code']" ).val()},
      dataType: json,
      type: 'post',
      success: function(data) {
        if(data=="true") {
          return true;
        }
        if (data=="false") {
          event.preventDefault();
          return false;
        }
      }
    });
  });
});

and finally the PHP (/func/check_captcha.php)

<?php

  require_once '../securimage/securimage.php';
  $securimage = new Securimage();

  if ($securimage->check($_POST['cc']) == false) {
    $return = "false";
  }
  else {
    $return = "true";
  }

  die(json_encode($return));  

?>

can anyone see any major error prones ?

Found the problem ... as usual something which get's lost after hours of programming and frying your brain :

dataType: json,

should be

dataType: "json",

at least that seems to solve the riddle until now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM