douzhang5199 2014-09-10 21:27
浏览 85
已采纳

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

this is my form as it sents the email to the four different email on selectoin of different radio buttons.....But i donot want the form to be submitted until the captcha is verified..so i added this captcha but it is not working I dont know why.Please help me on this... .Thanks in advance.

                <?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 . "
" . "Email: " . $email . "
" . "phone :" . $phone12 . "
" . "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条回答 默认 最新

  • donglin9068 2014-09-10 22:00
    关注

    Simply create an $error variable that is set to false. If the captcha is wrong, then set it to true.

    Only process the form if $error == false

    If you wanted to validate the captcha (not sure why) before the form is submitted, you would need to use ajax that calls the php script that verify's it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部