首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jquery的Captcha验证

问使用jquery的Captcha验证
EN

Stack Overflow用户
提问于 2014-03-31 05:47:40
回答 1查看 2.5K关注 0票数 0

我有一个应用程序表单,需要使用jquery验证captcha。如果输入的captcha是错误的,那么一个关于显示的警告框请再次输入captcha,输入的captcha是错误的.如何使用jquery?请帮帮我。我对jquery很陌生。

以下是代码:

代码语言:javascript
复制
      $(function() {
    $("#XISubmit").click(function(){
        event.preventDefault()
        val=$('#vercode').val()
        $.ajax({
            type:"post",
            url:"verify-captcha.php",
            data:{'code':val},
            success:function(data){
               if(data=='false'){
                    alert('Re-enter Captcha, You Entered wrong captcha code')
                    $("#cap").html("<img  src='captcha_image.php'>"); }
                else{  
                     document.getElementById("XIForm").submit();
                }
            }

        });    
    });
});


                document.getElementById("XIForm").submit();

                    });

**Html page :**

    <label>Security Validation</label>    

    <span><img   src="captcha_image.php"></span><input type="text" name="vercode" id="vercode" size="10" style="margin-top: -1px; float: left; width: 115px; margin-right: 12px;"></li><div id="msg"></div>

**captcha_image.php**
<?
// *** CAPTCHA image generation ***
// *** http://frikk.tk ***

session_start();

// *** Tell the browser what kind of file is come'n at 'em! ***
header("Content-Type: image/jpeg");

// *** Send a generated image to the browser ***
die(create_image());

// *** Function List ***
function create_image()
{
    // *** Generate a passcode using md5
    //  (it will be all lowercase hex letters and numbers ***
    $md5 = md5(rand(0,9999));
    $pass = substr($md5, 10, 5);

    // *** Set the session cookie so we know what the passcode is ***
    $_SESSION["pass"] = $pass;

    // *** Create the image resource ***
    $image = ImageCreatetruecolor(100, 20);

    // *** We are making two colors, white and black ***
    $clr_white = ImageColorAllocate($image, 255, 255, 255);
    $clr_black = ImageColorAllocate($image, 0, 0, 0);

    // *** Make the background black ***
    imagefill($image, 0, 0, $clr_black);

    // *** Set the image height and width ***
    imagefontheight(15);
    imagefontwidth(15);

    // *** Add the passcode in white to the image ***
    imagestring($image, 5, 30, 3, $pass, $clr_white);

    // *** Throw in some lines to trick those cheeky bots! ***
    imageline($image, 5, 1, 50, 20, $clr_white);
    imageline($image, 60, 1, 96, 20, $clr_white);

    // *** Return the newly created image in jpeg format ***
    return imagejpeg($image);

    // *** Just in case... ***
    imagedestroy($image);
}
?>

verify-captcha.php

代码语言:javascript
复制
<?php
session_start(); 
if ($_POST["code"] != $_SESSION["cap_code"] || $_SESSION["cap_code"]=='')  { 
 echo 'false';
}else{
echo 'true';}
EN

回答 1

Stack Overflow用户

发布于 2014-03-31 07:02:47

你能做的步骤

  1. 将提交按钮更改为按钮
  2. 在按钮单击,调用验证-captcha.php
  3. 如果captcha是好的,提交表单。
  4. 其他警报错误消息

下面是修改过的脚本(未经测试):

代码语言:javascript
复制
$(function() {
    $("#XISubmit").click(function(){
        event.preventDefault()
        val=$('#vercode').val()
        $.ajax({
            type:"post",
            url:"verify-captcha.php",
            data:{'code':val},
            success:function(data){
               if(data=='false'){
                    alert('Re-enter Captcha, You Entered wrong captcha code')
                    $("#cap").html("<img  src='captcha.php'>"); }
                else{  
                    $("#XIForm").submit();
                }
            }

        });    
    });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22754410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档