【问题标题】:Custom Contact Form with simple Math Captcha带有简单数学验证码的自定义联系表
【发布时间】:2015-03-27 17:09:37
【问题描述】:

我的网站上有一个运行良好的联系表。现在我正在尝试在 from 中包含一个数学验证码。不幸的是,我无法让它工作。知道如何(在哪里)包含验证码吗?

我尝试了很多东西,但都无法正常工作。也许代码不是最好的 :) 我已经在 stackoverflow 和 google 中搜索了解决方案,但没有运气。

我目前的工作联系表:

<?php
if(isset($_POST['submitted'])) {
if(trim($_POST['contactName']) === '') {
    $nameError = sprintf( __( 'Please enter your name.', 'test_theme' ) );
    $hasError = true;
} else {
    $name = trim($_POST['contactName']);
}

if(trim($_POST['email']) === '')  {
    $emailError = sprintf( __( 'Please enter your email address.', 'test_theme' ) );
    $hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
    $emailError = sprintf( __( 'You entered an invalid email address.', 'test_theme' ) );
    $hasError = true;
} else {
    $email = trim($_POST['email']);
}

if(trim($_POST['comments']) === '') {
    $commentError = sprintf( __( 'Please enter a message.', 'test_theme' ) );
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['comments']));
    } else {
        $comments = trim($_POST['comments']);
    }
}

if(!isset($hasError)) {
    $emailTo = get_option('tz_email');
    if (!isset($emailTo) || ($emailTo == '') ){
        $emailTo = get_option('admin_email');
    }
    $subject = sprintf( __( 'Contact Form', 'test_theme' ) );
    $body = "Name: $name \n\nMail: $email \n\n $comments";
    $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    wp_mail($emailTo, $subject, $body, $headers);
    $emailSent = true;
}

} ?>

                    <?php if(isset($emailSent) && $emailSent == true) { ?>
                        <div class="thanks">
                            <p><?php _e( 'Thanks, your email was sent successfully.', 'test_theme' ); ?></p>
                        </div>
                    <?php } else { ?>
                        <?php if(isset($hasError) || isset($captchaError)) { ?>
                            <p class="error-conform"><?php _e( 'Sorry, an error occured.', 'test_theme' ); ?><p>
                        <?php } ?>

                <form action="<?php the_permalink(); ?>" class="author-description" id="contactForm" method="post">
                            <h2><?php _e( 'Contact Us', 'test_theme' ); ?></h2><br />
                            <p><label for="contactName"><?php _e( 'Your Name', 'test_theme' ); ?> <span>*</span>
                            <?php if($nameError != '') { ?>
                                <span class="error-conform"><?=$nameError;?></span>
                            <?php } ?><br />
                            <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" /></label></p>

                            <p><label for="email"><?php _e( 'Your Email Adress', 'test_theme' ); ?> <span>*</span>
                            <?php if($emailError != '') { ?>
                                <span class="error-conform"><?=$emailError;?></span>
                            <?php } ?><br />

                            <input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" /></label></p>

                            <p><label for="commentsText"><?php _e( 'Your Message', 'test_theme' ); ?> <span>*</span>
                            <?php if($commentError != '') { ?>
                                <span class="error-conform"><?=$commentError;?></span>
                            <?php } ?><br />                                
                            <textarea type="text" name="comments" id="commentsText" rows="20" cols="30"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea></label></p>

                        <p><input type="submit"></p>

                    <input type="hidden" name="submitted" id="submitted" value="true" />
                </form>
            <?php } ?>  

验证码应包含在上述联系表格中:

第 1 部分:

if(!isset($_POST['submitted'])) {
    session_start();
    $digit1 = mt_rand(1,6);
    $digit2 = mt_rand(1,6);
    $math = "$digit1 + $digit2";
    $_SESSION['answer'] = $digit1 + $digit2;
}

第 2 部分:

    if ($_SESSION['answer'] != $_POST['answer'] ) {
    $mathError = 'Please answer the math question.';
    $hasError = true;
  }

第 3 部分:

    <span class="error-conform"><?=$mathError;?></span>
    <li><label>What's <?php echo $math; ?> = </label><input name="answer" type="text" /></li>

【问题讨论】:

    标签: php html forms


    【解决方案1】:

    我想这会给你一些想法。

    <?php
    session_start(); // session start should be the very first line of your php code, for session management
    
    // checking whether the form is being submitted by the user
    if( isset($_POST['submitted']) )
    {
        // checking whether the user submitted answer matches with the captcha
        if ($_SESSION['answer'] != $_POST['answer'] ) 
        {
            $mathError = 'Please answer the math question.';
            $hasError = true;
        }
    
        // ....
        // do other form validations here..
        // ....
    
        if( !$hasError )
        {
            // no errors, so send the mail or do whatever you want to do here...
        }
    }
    
    // now generating new captcha
    $digit1 = mt_rand(1,6);
    $digit2 = mt_rand(1,6);
    $math = "$digit1 + $digit2";
    $_SESSION['answer'] = $digit1 + $digit2;
    ?>
    <!-- html goes here --->
    
    <forma ction="<?php the_permalink(); ?>
        <!-- include the form fields here... -->
    
        <?php if(!empty($mathError)) { ?>
            <span class="error-conform"><?php echo $mathError; ?></span>
        <?php } ?>
    
        <label>What's <?php echo $math; ?> = </label><input name="answer" type="text" />
        <input type="hidden" name="submitted" id="submitted" value="true" />
    
    </form>
    
    <!-- rest of the html goes here --->
    

    【讨论】:

    • 非常感谢!有用。一个小问题,提交成功后输入字段(姓名、电子邮件、消息框)不清除。有什么想法吗?
    • 这是因为您实际上是在回显&lt;input&gt; 元素的value 属性上的值!所以你可以做的是,要么添加一个条件来检查电子邮件是否发送成功(可能是一个布尔标志)。如果为 TRUE,则不要在输入元素的 value 属性中回显这些值。否则回显。另一种方法是在邮件发送成功后重定向到另一个 html 页面(感谢页面)!
    • 好的,谢谢。我会试试。另一个小问题;)提交表单后,成功发送消息下方没有隐藏...好的,我将重定向到另一个页面!再次感谢您-我会接受答案。
    猜你喜欢
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    相关资源
    最近更新 更多