将验证码添加到表单无法正常工作

时间:2014-10-15 20:36:18

标签: php forms captcha

我尝试在此表单中添加验证码,如果您愿意,请检查链接。

This is the link from Stackoverflow

但是当我输入验证码错误时,我无法正常工作,但我仍然收到邮件。

<?php
session_start();
$cap = 'notEq';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if ($_POST['captcha'] == $_SESSION['cap_code']) {
        // Captcha verification is Correct. Do something here!
        $cap = 'Eq';
    } else {
        // Captcha verification is wrong. Take other action
        $cap = '';
    }



    $to = "exmaple@mail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $ip = $_POST['ip'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . " " . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.

}

?>

下面是验证码和提交按钮。

    <!-- Captcha -->
<input type="text" name="captcha" id="captcha" maxlength="6" size="6"/>


    <!-- Submit Button -->
<input type="submit" id="captcha" name="captcha" value="Submit">

那我在这里做错了什么?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的输入字段都具有相同的名称,因此文本字段的内容可能会被提交按钮的值覆盖。

更改提交按钮名称。

<input type="text" name="captcha" id="captcha" maxlength="6" size="6"/>

<!-- Submit Button -->
<input type="submit" id="captchaBut" name="captchaBut" value="Submit">