【问题标题】:HTML Form with PHP Captcha Plugin, I'm running into some issues带有 PHP Captcha 插件的 HTML 表单,我遇到了一些问题
【发布时间】:2013-04-14 03:35:04
【问题描述】:

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

我在我拥有的表单中使用 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));

任何一个+多个类似的组合都没有成功。

所以重申一下这个问题:
如何在需要时刷新表单/验证码

您提交验证码表单的习惯行为是什么?

谢谢。

【问题讨论】:

    标签: php javascript captcha forms


    【解决方案1】:

    你应该做这样的事情。本质上是检查错误并将错误推送到错误数组中,您可以稍后循环并显示给用户。提交后,您检查recaptcha,如果它无效,它将自动清除该字段。不要使用 alerts 或 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-12
      • 2022-11-23
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      • 2022-01-11
      相关资源
      最近更新 更多