HTML表单与PHP验证码插件,我遇到了一些问题


HTML Form with PHP Captcha Plugin, I'm running into some issues

我找过类似的问题,但没有找到我的特殊情况。

我使用的PHP验证码插件的形式,我有。我处理不正确和正确的验证码条目非常相似。如果用户的短语是正确的,我抛出一个javascript"成功"警报,发送表单电子邮件,然后将它们发送回最后一个页面。如果不正确,我抛出一个javascript "your incorrect"警报,并将它们发送回最后一页。

我的问题-如果他们是不正确的,我需要刷新验证码,因为有一个不正确的验证码条目,你总是需要刷新图像进行另一次尝试。此外,如果他们是正确的,我希望清除字段,但只有清除时正确,如果不正确,我要保持表单数据。我该怎么做呢?

这是我的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));

没有成功+多个类似的组合。

重申一下这个问题:
如何在需要时刷新表单/captcha

提交captcha表单时,您的实践行为是什么?

谢谢。

您应该这样做。本质上是检查错误并将错误压入错误数组,稍后可以循环并显示给用户。提交后,您检查验证码,如果无效,它将自动清除字段。不要使用警报或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>