0

好的,所以请在这里忍受我。

我已经像这样设置了我的 HTML 表单代码......

<script type="text/javascript">
     var RecaptchaOptions = {
        theme : 'clean'
     };
</script>                                   
<form method="post" action="signup-form-1.php">
    <label for="Name">Name:</label><br />
    <input type="text" name="Name" id="Name" placeholder="Your Name" required="required" /><br /><br />

    <label for="Email">Email Address:</label><br />
    <input type="email" name="Email" id="Email" placeholder="Your E-mail address" required="required" /><br /><br />

     <label for="Phone">Phone Number</label>
    <input type="tel" name="Phone" id="tel" placeholder="Numbers and Hyphens allowed" required="required" /><br /><br />

    <!--<label for="Subject">Phone Number:</label><br />
    <input type="text" name="Subject" id="Subject" /><br /><br />-->

    <label for="Age">Age:</label><br />
    <input type="text" name="Age" id="Age" placeholder="How Old Are You?" required="required" /><br /><br />

    <label for="InterestedIn">What Tournament are you interested in?</label><br /><br />
        <input type="checkbox" name="InterestedIn[]" value="op1"><p class="check1">option 1</p>
        <input type="checkbox" name="InterestedIn[]" value="op2"><p class="check1">option 2</p>
        <input type="checkbox" name="InterestedIn[]" value="op3"><p class="check1">option 3</p>
        <input type="checkbox" name="InterestedIn[]" value="op4"><p class="check1">option 4</p>


    <label for="Message">Message:</label><br />
    <textarea name="Message" rows="20" cols="20" id="Message" placeholder="Additional Information"></textarea><br /><br />

    <script type="text/javascript" src="http://api.recaptcha.net/challenge?k=recaptcha key 1"></script>
    <noscript>
        <iframe src="http://api.recaptcha.net/noscript?k=recaptcha key 2" height="300" width="500" frameborder="0" title="CAPTCHA test"></iframe>
        <br />
        <label for="tswcaptcha">Copy and paste the code provided in above box here:</label><br />
        <textarea name="recaptcha_challenge_field" id="tswcaptcha" rows="3" cols="40"></textarea>
        <input type="hidden" name="recaptcha_response_field" value="manual_challenge" />
    </noscript>

    <input type="submit" name="submit" value="Submit" class="submit-button" />
    <input type="reset" name="reset" value="Reset" class="reset-button" />

</form>

我的recaptcha 出现了,我可以输入单词,我的公钥和私钥都在正确的位置等等。到目前为止,一切都很好。

我的 PHP 代码看起来像这样....

<?php


$EmailFrom = Trim(stripslashes($_POST['Name'])); 
$EmailTo = "my.email@mail.com";
$Subject = 'Custom Subject Line here';
$Name = Trim(stripslashes($_POST['Name'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Phone = Trim(stripslashes($_POST['Phone'])); 
$Age = Trim(stripslashes($_POST['Age'])); 
$Message = Trim(stripslashes($_POST['Message'])); 
$InterestedIn = Trim(stripslashes($_POST['InterestedIn'])); 


// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Age: ";
$Body .= $Age;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$Body .= "InterestedIn: ";
$Body .= implode(", ", $_POST['InterestedIn']);
$Body .= "\n";


// send email 
$success = mail($EmailTo, $Subject, $Body, "From: $Name");



// redirect to success page 
if ($success){
  print "<meta http-

equiv=\"refresh\" content=\"0;URL=signup-thanks.htm\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=signup-error.htm\">";
}

?>

我的 PHP 现在解析数据并以电子邮件格式发送。我可以收到它,我什至得到用逗号分隔的复选框选项,一切正常。

我遇到的唯一问题是,即使没有填写recaptcha,表格仍然会发送。我知道我需要验证我的 PHP 文件中的验证码。问题是我不知道该怎么做。

在这一点上,我只是一名网络开发学生,我正在尝试一些我以前从未尝试过的东西——我们已经看到了基本的联系表格和基本的 PHP,但我们还没有处理复选框、内爆或验证——我到目前为止,我自己都想通了。

有人可以看看我的代码并告诉我如何将验证函数放在我的 PHP 文件中吗?非常欢迎详细信息和细节。

在过去的几天里,我尝试自己搜索这些信息,但我仍然不明白 - 我无法修改我找到的答案以满足我的需要。我还在学习。但我确实需要这样做,您的帮助将不胜感激。

有没有善良的灵魂愿意同情一个急切的n00b?

4

2 回答 2

1

根据此页面,您需要顶部的验证码。

试试这个(未经测试):

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

    if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
    } else {

        $EmailFrom = Trim(stripslashes($_POST['Name'])); 
        $EmailTo = "my.email@mail.com";
        $Subject = 'Custom Subject Line here';
        $Name = Trim(stripslashes($_POST['Name'])); 
        $Email = Trim(stripslashes($_POST['Email'])); 
        $Phone = Trim(stripslashes($_POST['Phone'])); 
        $Age = Trim(stripslashes($_POST['Age'])); 
        $Message = Trim(stripslashes($_POST['Message'])); 
        $InterestedIn = Trim(stripslashes($_POST['InterestedIn'])); 


        // prepare email body text
        $Body = "";
        $Body .= "Name: ";
        $Body .= $Name;
        $Body .= "\n";
        $Body .= "Email: ";
        $Body .= $Email;
        $Body .= "\n";
        $Body .= "Phone: ";
        $Body .= $Phone;
        $Body .= "\n";
        $Body .= "Age: ";
        $Body .= $Age;
        $Body .= "\n";
        $Body .= "Message: ";
        $Body .= $Message;
        $Body .= "\n";
        $Body .= "InterestedIn: ";
        $Body .= implode(", ", $_POST['InterestedIn']);
        $Body .= "\n";


        // send email 
        $success = mail($EmailTo, $Subject, $Body, "From: $Name");



        // redirect to success page 
        if ($success){
          print "<meta http-

        equiv=\"refresh\" content=\"0;URL=signup-thanks.htm\">";
        }
        else{
          print "<meta http-equiv=\"refresh\" content=\"0;URL=signup-error.htm\">";
        }
    }
?>
于 2013-09-30T20:16:15.010 回答
0

要在 HTML 端解决这个问题,您可以使用requiredattr。

例子 :

<input type="hidden" name="recaptcha_response_field" value="manual_challenge" required/>

要在 PHP 端解决这个问题,您可以使用 isset 和其他字符串控件

例子 :

if(isset($_GET['recaptcha_response_field']) && $_GET['recaptcha_response_field'] != "" && strlen($_GET['recaptcha_response_field']) > 5)
于 2013-09-30T20:14:49.683 回答