3

我正在使用以下内容将表格发送到电子邮件地址。我想合并recaptcha,但想稍微调整一下。

1)如果没有填写验证码,我希望错误弹出一个警告框。

2)如果验证码填写正确,那么它应该重新加载显示感谢消息的页面(位于下面脚本的底部)。

3)而不是检查 verify.php 文件,我希望它在这个页面上检查它。(我将操作中的永久链接替换为 verify.php 文件的链接 - 我不想这样做!)

        <form class="form" method="POST" action="<?php the_permalink(); ?>">
            <table border="0" style="float:left;" width="490">
                <tbody>
                    <tr>
                        <td>
                        <p>Company Name:</p>
                        </td>
                        <td>&nbsp;</td>
                        <td><input type="text" name="companyname" id="companyname" /></td>
                    </tr>
                    <tr>
                        <td>
                        <p>Your Name:</p>
                        </td>
                        <td>&nbsp;</td>
                        <td><input type="text" name="fullname" id="fullname" /></td>
                    </tr>
                    <tr>
                        <td>
                        <p>E-mail:</p>
                        </td>
                        <td>&nbsp;</td>
                        <td><input type="text" name="email" id="email" /></td>
                        <td colspan="2">&nbsp;</td>
                    </tr>
                    <tr>
                        <td>
                        <p>Telephone:</p>
                        </td>
                        <td>&nbsp;</td>
                        <td><input type="text" name="tel" id="tel" /></td>
                        <td colspan="2">&nbsp;</td>
                    </tr>
                </tbody>
            </table>

            <table border="0" style="float:left;" width="490">
                <tbody>
                    <tr>
                        <td valign="top"><p style="margin-right:47px;margin-top:7px;">Enquiry:</p></td>
                        <td><textarea name="enquiry"></textarea></td>
                    </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
<script type="text/javascript">
function validateCaptcha()
{
    if ($('input[name="valid"]')) return true;
    if ($('input[name="recaptcha_response_field"]').val() == "")
    {
        alert("Please complete the CAPTCHA field.");
        return false
    }
    $.ajax({
        url: "http://www.medilogicuk.com/wp-content/themes/default/verify.php",
        type: "POST",
        async:"false",
        data: {
            recaptcha_response_field: $('input[name="recaptcha_response_field"]').val(),
            recaptcha_challenge_field: $('input[name="recaptcha_challenge_field"]').val()
        },
        success: function(data){
            if (data == "OK")
            {
                $('input[name="valid"]').val(1);
                $('.form').submit();
            }
            else
            {
                alert(data);
            }
        },
        error: function(){
            alert("An error occured, please try again later");
        }
    });
    return false;
};
</script>


                <?php require_once('recaptchalib.php');
  $publickey = "(my key)"; // you got this from the signup page
  echo recaptcha_get_html($publickey);
?>
                    </td>
                </tr>    
                    <tr>
                        <td colspan="2"><button type="submit" name="submit" value="Send message">Send message</button></td>
                    </tr>
                </tbody>
            </table>        

        <? if(isset($_POST['submit'])) { 

        $to = "(my email)";
        $header = 'From: (company email)';
        $subject = "Website enquiry";
        $companyname_field = $_POST['companyname'];
        $fullname_field = $_POST['fullname'];
        $email_field = $_POST['email'];
        $tel_field = $_POST['tel'];
        $enquiry_field = $_POST['enquiry'];


        $body = "Hello,\n\n You have an enquiry from the website, please see the details below:\n\n Name: $fullname_field\n Company Name: $companyname_field\n E-Mail: $email_field\n Tel: $tel_field\n Message:\n $enquiry_field\n\n Please reply to the enquiry asap.\n\n Kind Regards \n";

        mail($to, $subject, $body, $header);

        echo "</br><p style=\"color:#e41770!IMPORTANT;\">Thank you for getting in touch, we will contact you shortly.</p>";

        } ?>
        </form> 

这是在我的 verify.php 文件中:

<?php
  require_once('recaptchalib.php');
  $privatekey = "(my key)";
  $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) 
{
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again. (reCAPTCHA said: " . $resp->error . ")");
} 
else 
{
    echo "OK";
}
  ?>
4

1 回答 1

2

表格更改

<form class="form" method="POST" action="index.php" onsubmit="return validateCaptcha()">
<input type="hidden" name="valid" value="0" />

表格下方

<script type="text/javascript">
function validateCaptcha()
{
    if ($('input[name="valid"]')) return true;
    if ($('input[name="recaptcha_response_field"]').val() == "")
    {
        alert("Fill in the captcha field");
        return false
    }
    $.ajax({
        url: "verify.php",
        type: "POST",
        async:"false",
        data: {
            recaptcha_response_field: $('input[name="recaptcha_response_field"]').val(),
            recaptcha_challenge_field: $('input[name="recaptcha_challenge_field"]').val()
        },
        success: function(data){
            if (data == "OK")
            {
                $('input[name="valid"]').val(1);
                $('.form').submit();
            }
            else
            {
                alert(data);
            }
        },
        error: function(){
            alert("An error occured, please try again later");
        }
    });
    return false;
};
</script>

验证.php

if (!$resp->is_valid) 
{
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again. (reCAPTCHA said: " . $resp->error . ")");
} 
else 
{
    echo "OK";
}
于 2012-06-25T09:35:12.397 回答