如何为captcha添加重新加载链接?

时间:2012-01-26 19:37:03

标签: php javascript ajax forms captcha

我已经对这个问题进行了一些研究,但是每个php形式都彼此不同,所以其他人的解决方案可能对我不起作用。 我希望有一个链接,说“重新加载验证码”,当它点击它重新加载而不刷新页面。 我的表单确实使用了AJAX和javascript,我猜这是为了做到这一点所需要的。

我的表单的javascript就像:

jQuery(document).ready(function() {
$('.contactform').submit(function() {
    var action = $(this).attr('action');
    var form = this;
    $('.submit', this).attr('disabled', 'disabled').after(
          '<img src="assets/ajax-loader.gif" class="loader" />');
    $('.message', this).slideUp(750, function() {
        $(this).hide();
        $.post(action, {
            name: $('.name', form).val(),
            email: $('.email', form).val(),
            phone: $('.phone', form).val(),
            comments: $('.comments', form).val(),
            verify: $('.verify', form).val()
        },
        function(data) {
            $('.message', form).html(data);
            $('.message', form).slideDown('slow');
            $('img.loader', form).fadeOut('fast', function() {
                $(this).remove();
            });
            $('.submit', form).removeAttr('disabled');
            if (data.match('success') != null)
            $('.message', form).show().delay(5000).fadeOut();
        });
    });
    return false;
});
});

PHP表格如下:

 <?php if (!isset($_SESSION)) session_start();

 if(!$_POST) exit;

if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");


$address = "email@domain.com";
$bcc = "email@domain.com";

    $twitter_active     = 0;
    $twitter_user       = ""; // Your user name
    $consumer_key       = "";
    $consumer_secret    = "";
    $token              = "";
    $secret             = "";

$name    = $_POST['name'];
$email  = $_POST['email'];
$phone  = $_POST['phone'];
$dayin  = $_POST['dayin'];
$dayout = $_POST['dayout'];
$comments = $_POST['comments'];

if (isset($_POST['verify'])) :
    $posted_verify   = $_POST['verify'];
    $posted_verify   = md5($posted_verify);
else :
    $posted_verify = '';
endif;

// Important Variables
$session_verify = $_SESSION['verify'];

if (empty($session_verify)) $session_verify = $_COOKIE['verify'];

$error = '';

    if(trim($name) == '') {
        $error .= '<li>Your name is required.</li>';
    }

    if(trim($email) == '') {
        $error .= '<li>Your e-mail address is required.</li>';
    } elseif(!isEmail($email)) {
        $error .= '<li>You have entered an invalid e-mail address.</li>';
    }

    if(trim($phone) == '') {
        $error .= '<li>Your phone number is required.</li>';
    } elseif(!is_numeric($phone)) {
        $error .= '<li>Your phone number can only contain digits (numbers 
 and no spaces).</li>';
    }

    if(trim($comments) == '') {
        $error .= '<li>You must enter a message to send.</li>';
    }

    if($session_verify != $posted_verify) {
        $error .= '<li>The verification code you entered is incorrect.
  </li>';
    }

    if($error != '') {
        echo '<div class="error_message">Attention! Please correct the 
  errors below and try again.';
        echo '<ul class="error_messages">' . $error . '</ul>';
        echo '</div>';

    } else {

    if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); }

     $e_subject = 'Website Enquiry';
     $msg = '<html><body>    </body></html>';

    if($twitter_active == 1) {

        $twitter_msg = $name . " - " . $comments . ". You can contact " . 
  $name . " via email, " . $email ." or via phone " . $phone . ".";
        twittermessage($twitter_user, $twitter_msg, $consumer_key, 
  $consumer_secret, $token, $secret);

    }

    $msg = wordwrap( $msg, 70 );

    $headers = "From: $email\r\nBCC:{$bcc}\r\n" . PHP_EOL;
    $headers .= "Reply-To: $email" . PHP_EOL;
    $headers .= "MIME-Version: 1.0" . PHP_EOL;
    $headers .= "Content-type: text/html; charset=utf-8" . PHP_EOL;
    $headers .= 'Content-Transfer-Encoding: 8bit'. "\n\r\n" . PHP_EOL;

    if(mail($address, $e_subject, $msg, $headers)) {

     echo "<fieldset>";
     echo "<div id='success_page'>";
     echo "<img src='success.png' align='absmiddle' style='padding-right:5px;' 
 /><strong>Email Sent Successfully.</strong>";
     echo "</div>";
     echo "</fieldset>";

     } else {

     echo 'ERROR!'; // Dont Edit.

     }

}

 function twittermessage($user, $message, $consumer_key, $consumer_secret, $token, 
 $secret)     { // Twitter Direct Message function, do not edit.

require_once('twitter/EpiCurl.php');
require_once('twitter/EpiOAuth.php');
require_once('twitter/EpiTwitter.php');

$Twitter = new EpiTwitter($consumer_key, $consumer_secret);
$Twitter->setToken($token, $secret);

$direct_message = $Twitter->post_direct_messagesNew( array('user' => $user, 'text' 
=> $message) );
$tweet_info = $direct_message->responseText;

}

?>

(我把$ msg中的电子邮件发送出去,因为代码太多了,我认为没有必要包含它)

我的表单上的验证码的HTML如下:

<label for="verify" accesskey="V">&nbsp;&nbsp;&nbsp;<img src="image.php" 
alt="Image verification" border="0"/></label>

<input name="verify" type="text" id="verify" class="verify" size="6" value="" 
style="width: 50px;" />

任何人都知道我应该逐步做什么? (注意:我仍然是php和ajax的业余爱好者)...哦,不,如果你想的话,我不想使用reCaptcha:)

1 个答案:

答案 0 :(得分:0)

这应该给你一个headstart,这个asssums image.php生成图像并更新session / cookie变量。我们更新图像以包含缓存清除字符串,以便它应该更新图像和会话变量。

<强> HTML

<button id="reloadCaptcha">Reload Captcha</button>

更改HTML

<img id="captchaImage" src="image.php" alt="Image verification" border="0"/>

<强> JS

$('button#reloadCaptcha').click(function() {
    $('img#captchaImage').attr('src', 'image.php?'+new Date().getTime());
});

显然未经测试,但应该给你一个良好的实施开始。