我创建了一个Captcha,但它不起作用。我不知道为什么?

时间:2014-09-11 05:27:13

标签: php forms validation

这是我的表格,因为它将电子邮件发送到不同单选按钮的selectoin上的四个不同的电子邮件.....但我不希望提交表单直到验证验证码。所以我添加了这个验证码但是它不起作用我不知道为什么。请帮助我...。提前谢谢。

                <?php
                session_start();
                //error_reporting(E_ERROR | E_PARSE);

                //echo "<pre>";
                //print_r($_REQUEST);
                if(!empty($_POST['answer']) )
                {
                    //echo "You entered " . htmlentities($_POST['answer']) . " which is ";

                    if ($_SESSION['answer'] == $_POST['answer'])
                    {
                        //echo 'correct';
                        $validatedCaptcha = true;
                    }
                    else
                    {
                        echo '<center>'.'You Filled wrong Captcha. We expected ' . $_SESSION['answer'].'      .Kindly Fill the Form Again'.'</center>';
                        $validatedCaptcha = false;
                    }
                }

                $digit1 = mt_rand(1, 20);
                $digit2 = mt_rand(1, 20);
                if (mt_rand(0, 1) === 1) {
                    $math = "$digit1 + $digit2";
                    $_SESSION['answer'] = $digit1 + $digit2;
                } else {
                    if($digit1 >= $digit2)
                    {
                        $math = "$digit1 - $digit2";    
                        $_SESSION['answer'] = $digit1 - $digit2;
                    }
                    else if($digit1 < $digit2)
                    {
                        $math = "$digit2 - $digit1";    
                        $_SESSION['answer'] = $digit2 - $digit1;
                    }
                }
                //echo "<pre>";
                //print_r($_SESSION);
                ?>
                <center>
                    <?php
                    $name = $_REQUEST['name11'];
                    $email = $_REQUEST['email11'];
                    $phone12 = $_REQUEST['mobile11'];
                    $subject = $_REQUEST['subject11'];
                    $message1 = $_REQUEST['message11'];
                    $zone1 = $_REQUEST['zone1'];
                    $null_virdi = "-f " . $email;


                    if (isset($_REQUEST['submit']) && $validatedCaptcha == true ) {

                        $message = "Name: " . $name . "\n" . "Email: " . $email . "\n" . "phone :" . $phone12 . "\n" . "Message :" . $message1;

                        if ($_REQUEST['zone1'] == "South") {

                            echo "Thank you for Contacting our South Indian Zonal Office";
                            mail("abc@gmail.com", $subject, $message, null, $null_virdi);
                        }

                        if ($_REQUEST['zone1'] == "Delhi") {

                            mail("abc@gmail.com", $subject, $message, null, $null_virdi);
                            echo "Thank you for Contacting our Delhi Office";
                        }

                        if ($_REQUEST['zone1'] == "Hyderabad") {

                            mail("abc3@gmail.com", $subject, $message, null, $null_virdi);
                            echo "Thank you for Contacting our Hyderabad & AP Office";
                        }

                        if ($_REQUEST['zone1'] == "Pune") {

                            mail("abc4@gmail.com", $subject, $message, null, $null_virdi);
                            echo "Thank you for Contacting our Pune/Nasik Office";
                        }

                        if ($_REQUEST['zone1'] == "west") {

                            mail("abc5@gmail.com", $subject, $message, null, $null_virdi);
                            echo "Thank you for Contacting our West Zone Office";
                        }
                    }
                    ?>
                    <html><head>
                            <script>
                                function validateForm()
                                {
                                    var x = document.forms["myForm"]["email1"].value;
                                    var atpos = x.indexOf("@");
                                    var dotpos = x.lastIndexOf(".");
                                    if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length)
                                    {
                                        alert("Not a valid e-mail address");
                                        return false;
                                    }
                                }
                            </script>
                        </head><body>

                           <div style="width:50%;border: 4px double #dbd395;margin: 0;padding: 28px 0px 0px 34px;background-color: #F8F8F8">   
                 <form name="myForm" action="" onSubmit="return validateForm();" method="post" >

                             <table >
                                    <tr>
                                    <h3 style="margin-left:5px;">Enquiry for Quotation</h3>
                                    </tr>
                                    <tr>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td>Name&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;<input type="text" required="required" name="name11"  size="40"/></td>
                                    </tr>
                                    <td></td>
                                    </tr>
                                    <tr>
                                        <td>Email&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;<input type="text" required="required" name="email11" size="40"/></td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td>Mobile&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;<input type="text" required="required" name="mobile11" size="40"/></td>
                                    </tr>
                                    <tr>
                                        <td>Zone&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<input type="radio" required="required" name="zone1" value="South"/>South India &nbsp;&nbsp;<input type="radio" name="zone1" value="Delhi" />Delhi NCR <input type="radio" name="zone1" value="Hyderabad"/>Hyderabad & AP <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="zone1" value="Pune"/>Pune/Nashik <input type="radio" name="zone1" value="west"/>Other </td>
                                    </tr>
                                    <td></td>
                                    </tr>
                                    <tr>
                                        <td>Subject&nbsp;<font color="#FF0000">*</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;<input type="text" required="required" name="subject11" size="40"/></td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td style="vertical-align: top;"><font style=vertical-align:top; color="#000000">Message</font><font style=vertical-align:top; color="#FF0000">* :</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<textarea rows="4" cols="32" name="message11" required="required"/></textarea></td>
                                    </tr>
                <tr><td><center><b>Captcha</b></center></td></tr>
                                    <tr><td align="center">What's <?php echo $math; ?> = <input size="5" name="answer" type="text" /><br /></td></tr>

                                    <tr>
                                        <td align="center">


                                            <input  type="submit" name="submit" value="Send Message"/></td>
                                    </tr>

                                </table>

                            </form>
                </div>
                        <body/>
                    </html>
                </center>

1 个答案:

答案 0 :(得分:0)

只需创建一个设置为false的$error变量。如果验证码错误,则将其设置为true。

仅处理表单if $error == false

如果你想在提交表单之前验证验证码(不确定原因),你需要使用调用验证它的php脚本的ajax。