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


How to add a reload link for captcha?

我对这件事做了一些研究,但每个php表单都不同,所以其他人的解决方案可能不适合我。我想有一个链接,上面写着"重新加载captcha",当它被点击时,它会重新加载,而不会刷新页面。我的表单确实使用了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,因为它的代码太多了,我认为没有必要包含它)

我表单上captcha的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

这应该会给你一个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());
});

显然未经测试,但应该会为您实现它提供一个良好的开端。