PHP联系表单提交时无需填写所有字段

时间:2017-05-15 09:10:00

标签: javascript php jquery html css

我有一个PHP联系表单,即使字段没有填写也可以使用。直到最近我每天都开始收到一些空白电子邮件时,这个问题还没有解决。

如何在使用提交按钮之前强制在表单中填写所有字段?

以下是我的PHP代码:

    <?php

header("Access-Control-Allow-Origin: *");

$EmailFrom = "myemail";
$EmailTo = "myemail";
$Subject = "subject goes here";
$Email = Trim(stripslashes($_POST['email'])); 
$Message = Trim(stripslashes($_POST['message'])); 

// validation
$validationOK=true;
if (!$validationOK) { 
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    exit;
}

// prepare email body text
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email  
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom" . "\r\n" );

// redirect to success page 
if ($success){
   print "<meta http-equiv=\"refresh\"   content=\"0;URL=contactthanks.php\">";
}
else{
   print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

这是我的HTML标记:

                <!-- CONTACT FORM -->
                <div class="span9 contact_form">
                    <div id="note"></div>
                    <div id="fields">
                        <div id="post-ajax" style="display: none;"></div>
                        <form id="contact-form-face" class="clearfix" action="/php/contactengine.php">
                            <input type="text" name="email" value="Email" onFocus="if (this.value == 'Email') this.value = '';" onBlur="if (this.value == '') this.value = 'Email';" />
                            <textarea name="message" onFocus="if (this.value     == 'Message') this.value = '';" onBlur="if (this.value == '') this.value = 'Message';">Message</textarea>
                            <input class="contact_btn" name="submit" type="submit" value="Send Message" />
                        </form>
                    </div>
                </div>
                <!-- //CONTACT FORM -->

2 个答案:

答案 0 :(得分:1)

我找不到你的字段。但总的来说,HTML5提供了一种非常方便的方法来制作表单字段。为此,您可以在表单元素中添加required属性,例如:

<input type="text" name="txt_name" required />

现代浏览器将在表单提交期间验证字段。要支持旧版浏览器,可以使用JS验证库进行客户端验证,并使用PHP条件检查,例如: if(!empty($_POST['txt_name']))用于服务器端验证。

此外,建议不要使用元刷新标签进行重定向;例如,使用header('Location: error.htm'); exit;代替。

<!-- CONTACT FORM -->
<div class="span9 contact_form">
    <div id="note"></div>
    <div id="fields">
        <div id="post-ajax" style="display: none;"></div>
        <form id="contact-form-face" class="clearfix" action="/php/contactengine.php">
            <input type="text" name="email" value="Email" onFocus="if (this.value == 'Email') this.value = '';" onBlur="if (this.value == '') this.value = 'Email';" required />
            <textarea name="message" onFocus="if (this.value     == 'Message') this.value = '';" onBlur="if (this.value == '') this.value = 'Message';" required>Message</textarea>
            <input class="contact_btn" name="submit" type="submit" value="Send Message" />
        </form>
    </div>
</div>
<!-- //CONTACT FORM -->

答案 1 :(得分:0)

除了添加另一个答案中指出的required属性(通过检查元素可以很容易地传递)之外,您还需要在处理之前在服务器端进行验证。

您可以创建必填字段的数组,然后检查这些字段是否已设置且不为空。

<?php

        $errors = ""; 
        $requiredFields = array("email","message"); // enter the name in the inputs, ie name="someInput"

        foreach($requiredFields as $fieldname){

            if(!isset($_POST[$fieldname]) && empty($_POST[$fieldname])){

                $errors++;

                echo "Enter all fields";

                //OR redirect to error page

            }
        }

        if($errors <=0){

            // Proccess the form

            $EmailFrom = "myemail";
            $EmailTo = "myemail";
            $Subject = "subject goes here";
            $Email = Trim(stripslashes($_POST['email'])); 
            $Message = Trim(stripslashes($_POST['message']));

            // prepare email body text
            $Body .= "Email: ";
            $Body .= $Email;
            $Body .= "\n";
            $Body .= "Message: ";
            $Body .= $Message;
            $Body .= "\n";

            // send email  
            $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom" . "\r\n" );

            // redirect to success page 
            if ($success){

               header("location:contactthanks.php");
               exit();
            }
            else{
                header("location:error.htm")

                exit();
            }
        }
?>
相关问题