单击按钮时执行php

时间:2015-06-23 02:58:32

标签: php html forms

我有一个与contact.php相关联的联系表单。我已经在表单中添加了一个小的验证码小部件。我知道垃圾邮件机器人仍可以获得此功能,但它仅用于学习目的。

无论如何,我创建了两个随机数,用户必须在它附近的textarea中输入相应的答案。问题是,当点击提交以检查验证码是否正确时,我无法运行PHP。

很抱歉,如果代码有点乱,但它只是一个包含姓名,电子邮件和消息+按钮的简单表单。

P.S:$total仅用于阻止chrome在尝试刷新时显示确认表单重新提交消息。

更新 首先感谢所有的更正,我是新来的。 其次它只有一半解决了。这是因为当我从contact.php删除<form action="contact.php" method="post" name="contact_form">时,才会显示验证码验证提醒消息。除此之外,由于我对网络编程还不熟悉,有没有其他编程语言(最好在这种情况下使用)我可以使用,以便我可以创建验证码,检查它并放置像{{1}这样的注释在文本框的顶部(如果验证码不正确)并阻止用户提交表单,直到值正确为止?

Your captcha is incorrect

Printscreen:http://i.stack.imgur.com/u0Ca9.png

contact.php内容

<?php
// init session to store captcha numbers
session_start() 
?>

<form action="contact.php" method="post" name="contact_form">
<div class="contact" id="contact">
    <div class="container">
        <h2>Contact Us For a Quote</h2>
        <div class="contact-info">
            <ul>
                <li class="first">
                    <div class="position">
                        <a class=" icon user"></a><input name="name"     type="text" class="text" value="Name" onfocus="this.value = '';" onblur="if     (this.value == '') {this.value = 'Name';}">
                        <div class="clear"></div>
                        <div class="dot">
                            <span> </span>
                        </div>
                    </div>
                </li>
                <li class="first">
                    <div class="position">
                        <a class=" icon email"></a><input name="email" type="text" class="text" value="Email" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email';}">
                        <div class="clear"></div>
                        <div class="dot">
                            <span> </span>
                        </div>
                    </div>
                </li>
                <div class="clearfix"> </div>
                <li class="second">
                    <div class="position">
                        <a class=" icon msg"></a><textarea name="message" value="Your Message/request" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Your Message/request';}">Your Message/request</textarea>
                        <div class="clear"></div>
                        <div class="dot1">
                            <span> </span>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
        <div class= "verification">
            <center>
                <?php
                $min = 1;
                $max = 7;
                $num1 = rand ($min, $max);
                do{
                    $num2 = rand ($min, $max);
                }while($num1 == $num2);
                // store captcha numbers to check them on submit
                $_SESSION['num1'] = $num1;
                $_SESSION['num2'] = $num2;
                echo $num1 . " + " . $num2 . " =";
                $total = $num1 + $num2;
                ?>
                <input name="ver" type="text" value="" onfocus="this.value = '';" size="2" maxlength="2">
            </center>
        </div>

        <center><input type="submit" name="submit" onclick="myFunction()" value="Submit Request"></center>
        <?php
        // your form pass data through post, not get
        if(isset($_POST['submit'])){
            //submit clicked
            $verification = $_POST['ver'];
            // check numbers on submit
            if($_SESSION['num1'] + $_SESSION['num2'] != $verification)
            {
                echo'<script type="text/javascript">alert("Incorrect verification code");</script>';
            }
        }
        ?>
    </div>
</div>

2 个答案:

答案 0 :(得分:1)

问题是$ num1和$ num2变量没有定义你的检查验​​证码,因为不同的请求之间不保存变量状态。

您应该使用类似会话的smth来保存变量值。请参阅以下代码段中的评论:

<?php
  // init session to store captcha numbers
  session_start() 
?>

<form action="contact.php" method="post" name="contact_form">
    <div class="contact" id="contact">
        <div class="container">
            <h2>Contact Us For a Quote</h2>
            <div class="contact-info">
                <ul>
                    <li class="first">
                        <div class="position">
                            <a class=" icon user"></a><input name="name" type="text" class="text" value="Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Name';}">
                            <div class="clear"></div>
                            <div class="dot">
                                <span> </span>
                            </div>
                        </div>
                    </li>
                    <li class="first">
                        <div class="position">
                            <a class=" icon email"></a><input name="email" type="text" class="text" value="Email" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email';}">
                            <div class="clear"></div>
                            <div class="dot">
                                <span> </span>
                            </div>
                        </div>
                    </li>
                    <div class="clearfix"> </div>
                    <li class="second">
                        <div class="position">
                            <a class=" icon msg"></a><textarea name="message" value="Your Message/request" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Your Message/request';}">Your Message/request</textarea>
                            <div class="clear"></div>
                            <div class="dot1">
                                <span> </span>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
            <div class= "verification">
                <center>
                    <?php
                    $min = 1;
                    $max = 7;
                    $num1 = rand ($min, $max);
                    do{
                        $num2 = rand ($min, $max);
                    }while($num1 == $num2);
                    // store captcha numbers to check them on submit
                    $_SESSION['num1'] = $num1;
                    $_SESSION['num2'] = $num2;
                    echo $num1 . " + " . $num2 . " =";
                    $total = $num1 + $num2;
                    ?>
                    <input name="ver" type="text" value="" onfocus="this.value = '';" size="2" maxlength="2">
                </center>
            </div>

            <center><input type="submit" name="submit" onclick="myFunction()" value="Submit Request"></center>
            <?php
            // your form pass data through post, not get
            if(isset($_POST['submit'])){
                //submit clicked
                $verification = $_POST['ver'];
                // check numbers on submit
                if($_SESSION['num1'] + $_SESSION['num2'] != $verification)
                {
                    echo'<script type="text/javascript">alert("Incorrect verification code");</script>';
                }
            }
            ?>
        </div>
    </div>

答案 1 :(得分:0)

试试这个

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">