表单提交过程中的php确认消息

时间:2014-03-19 21:59:43

标签: javascript php html forms popup

我已经通过点击提交按钮创建了一个我要验证的表单,如果一切正常,则将输入存储在数据库中。验证和DB中的存储都可以正常工作。为了方便用户,我还提出了两个弹出窗口,一个用于成功提交,另一个用于发生错误。

我的问题是,当我第一次加载我的表单所在的页面时,我会收到与验证正确且数据已正确提交时相同的弹出窗口。

你能帮帮我吗?我希望只有在按下提交按钮后才能显示这些弹出窗口。

这是我的PHP代码:

验证代码

<?php  
    $firstNameError = $lastNameError = $streetError = $cityError = $postcodeError = $emailError = $mobileNumberError = $dateError = "";
    $firstName = $lastName = $street = $city = $postcode = $email = $mobileNumber = $date = $comments = "";
    if ($_SERVER["REQUEST_METHOD"] == "POST"){
        if (empty($_POST["firstName"]))
            {$firstNameError = "Please fill this field";}
            else
            {
                $firstName = test_input($_POST["firstName"]);
                // check if name only contains letters and whitespace
                if (!preg_match("/^[a-zA-Z ]*$/",$firstName))
                {
                    $firstNameError = "Please use only letters and white spaces";
                }
            }
            if (empty($_POST["lastName"]))
            {$lastNameError = "Please fill this field";}
            else
            {
                $lastName = test_input($_POST["lastName"]);
                // check if name only contains letters and whitespace
                if (!preg_match("/^[a-zA-Z ]*$/",$lastName))
                {
                    $lastNameError = "Please use only letters and white spaces"; 
                }
            }   
            if (empty($_POST["street"]))
            {$streetError = "Please fill this field";}
            else 
            {
                // check if street address syntax is valid
                $street = test_input($_POST["street"]);
                if(!preg_match('/[^A-Za-z0-9]/', $street)){
                    $streetError = "Please use only letters, numbers and whitespaces";
                }
            }   
            if (empty($_POST["city"]))
            {$cityError = "Please fill this field";}
            else
            {
                // check if city syntax is valid
                $city = test_input($_POST["city"]);
                // check if name only contains letters and whitespace
                if (!preg_match("/^[a-zA-Z ]*$/",$city))
                {
                    $cityError = "Please use only letters and whitespaces"; 
                }
            }
            if (empty($_POST["postcode"]))
            {$postcodeError = "Please fill this field";}
            else 
            {
                //check if postcode syntax is valid
                $postcode = test_input($_POST["postcode"]);
                if(!preg_match('/[^A-Za-z0-9]*$/', $postcode)){
                    $postcodeError = "Please use only letters, numbers and whitespace";
                }
            }
            if (empty($_POST["email"]))
            {$emailError = "Please fill this field";}
            else
            {
                $email = test_input($_POST["email"]);
                // check if e-mail address syntax is valid
                if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
                {
                    $emailError = "Please rewrite your email address in the format a@b.code"; 
                }
            }
            if (empty($_POST["mobileNumber"]))
            {$mobileNumberError = "Please fill this field";}
            else 
            {
                $mobileNumber = test_input($_POST["mobileNumber"]);
                // check if mobile number syntax is valid
                if (!preg_match('/^\d+$/',$mobileNumber))
                {
                    $mobileNumberError = "Please write only numbers";
                }
            }
            if (empty($_POST["date"]))
            {$dateError = "Please fill this field";}
            else
            {
                $date = test_input($_POST["date"]);
                //modified based on http://stackoverflow.com/questions/13194322/php-regex-to-check-date-is-in-yyyy-mm-dd-format
                if(!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date))
                {
                    $dateError = "Please follow this format:yyyy-mm-dd";
                }
            }
            $comments = test_input($_POST["comments"]);
    }
    function test_input($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }   
?>

存储在数据库中的代码

<?php
    if (empty($firstNameError) && empty($lastNameError) && empty($streetError) && empty($cityError) && empty($postcodeError) && empty($emailError) && empty($mobileNumberError) && empty($dateError)){
    mysql_connect("ServerName", "username", "password") or die("Connect failed!");
    mysql_select_db("DbName") or die(mysql_error());
    $appointment = "INSERT INTO inquiries 
                 (submit_date, first_name, last_name, address, postcode, email, phone, wedding_date, wedding_location, special_req) 
                 VALUES 
                 ('$currentDate', '$firstName', '$lastName', '$street', '$postcode', '$email', '$mobileNumber', '$date', '$city', '$comments')              ";              
    $result = mysql_query($appointment) or die(mysql_error());
    $message = "Your details have been submitted successfully. We will contact you soon!";
            echo "<script>
            alert('$message');
        </script>";
        }
           else{
        $message = "Oooops! Something went wrong. Please, resubmit the form based on the indications appeared under each field.";
        echo "<script>
            alert('$message');
        </script>";
        }
    mysqli_close($connection);
?>

谢谢!

1 个答案:

答案 0 :(得分:1)

我认为你的行

if (empty($firstNameError) && empty($lastNameError) && empty($streetError) && empty($cityError) && empty($postcodeError) && empty($emailError) && empty($mobileNumberError) && empty($dateError)){

应该检查不是空的

if (!empty($firstNameError) && !empty($lastNameError) && !empty($streetError) && !empty($cityError).....

否则脚本将继续进行查询