使用PHP Captcha插件的HTML表单,我遇到了一些问题

时间:2013-04-13 18:26:27

标签: php javascript captcha html-form

我已经找到了类似的问题而我没有找到我的具体案例。

我在我拥有的表单中使用PHP Captcha插件。我处理非常相似的错误和正确的验证码条目。如果用户的短语是正确的,我会抛出一个javascript“成功”提醒,发送表单电子邮件,然后将它们发送回最后一页。如果不正确,我会抛出一个javascript“你的错误”警告并将它们发送回最后一页。

我的问题 - 如果它们不正确我需要刷新Captcha,因为使用不正确的Captcha条目,您将始终需要刷新图像以进行另一次尝试。另外,如果它们是正确的,我希望字段清除但只在正确时清除,如果不正确我想保留表单数据。我怎么能这样做?

这是我的PHP验证码,您可以看到我的尝试。如果你想要html,请告诉我...(我也在POST之前用JS检查了所有条目)

<?php
/*set email*/
  $myemail  = "email@email.com";

  /* Check all form inputs using check_input function */
$name = $_POST['name'];
$companyName  = $_POST['companyName'];
$phone    = $_POST['phone'];
$email  = $_POST['email'];
$TimeForContact   = $_POST['TimeForContact'];
$aval = $_POST['aval'];
$messageFromFrom = $_POST['message'];
$firstTime = $_POST['firstTime'];
$subject = "Some Subject";

  require_once('recaptcha-php-1.11/recaptchalib.php');
  $privatekey = "*someprivatekey*"; //I took this out for the question
  $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

echo '<script type="text/javascript"> alert ("You have inserted the wrong phrase in the Captcha box. Please try again! Thank you."); window.history.back();</script>';
          exit();

  } else {

  $message = "some message

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

          echo '<script type="text/javascript"> alert ("Success! You have emailed your submission. Please await our response. Thank you."); window.history.back()</script>';


exit();
  }

?>

我最初尝试通过JS重新加载页面,例如:

window.reload(history.back());

window.location.reload(history.go(-1));

使用+多种类似的组合都没有成功。

重申问题:
如何在需要时刷新表单/验证码

OR

您提交验证码表格的行为是什么?

谢谢。

1 个答案:

答案 0 :(得分:0)

你应该做这样的事情。基本上检查错误并将错误推送到错误数组中,以后可以循环并显示给用户。提交后,您检查recaptcha,如果无效,它将自动清除该字段。不要使用alert或exit()或任何类似的东西,一个简单的$ errors数组就足够了。

//untested code
<?php

//check to make sure they submitted the form
if(isset($_POST['submit'])){

    $errors = array();

    $myemail  = "email@email.com";
    $name = $_POST['name'];
    $companyName  = $_POST['companyName'];
    /* ... */

    //recaptcha check
    require_once('recaptcha-php-1.11/recaptchalib.php');
    $privatekey = "*someprivatekey*";
    $resp = recaptcha_check_answer (
        $privatekey,$_SERVER["REMOTE_ADDR"],
        $_POST["recaptcha_challenge_field"],
        $_POST["recaptcha_response_field"]
    );

    //validate all data here
    if (!$resp->is_valid) {
        array_push($errors, "recaptcha invalid please try again");
    }else if( /* name is invalid */) {
        array_push($errors, "name invalid");
    }else if (){
        /* keep validing ...... with else if*/
    }

    /*....*/

    else{
        //everything validated so were good
        mail($myemail, $subject, $message);  
    }

}

?>

<html>
<head>
    <title></title>
</head>
<body>

    <form action="process.php">
        <!-- Your form here, "process.php" is this page itself -->
        <input name="submit" type="submit" value="Send">
    </form>

</body>
</html>