使用PHP验证不会提交包含必填字段的联系表单

时间:2013-12-18 18:18:34

标签: php html forms validation

我的联系表格不会提交并发送到我的电子邮件地址..

以下是我用来检查必填字段然后发送到我的电子邮件的PHP验证:

<?php   

if (isset($_GET['submit'])) {
    $body = '';

$body .= 'Name: ' . $_POST['name'] . "\n\n";
$body .= 'Phone: ' . $_POST['phone'] . "\n\n";
$body .= 'Email: ' . $_POST['email'] . "\n\n";
$body .= 'Message: ' . $_POST['message'] . "\n\n";

mail('myemailaddress@gmail.com', 'Contact Form', $body, 'From: no-reply@mycompany.com');
}   

// define variables and initialize with empty values
$nameErr = $addressErr = $emailErr = $messageErr = $spamcheckErr = "";
$name = $address = $email = $message = $spamcheck = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $nameErr = "Please enter your name.";
    }
    else {
        $name = $_POST["name"];
    }

    if (empty($_POST["email"])) {
        $emailErr = "Please enter your email.";
    }
    else {
        $email = $_POST["email"];
    }

    if (empty($_POST["message"]))  {
        $messageErr = "Cannot leave message box blank.";
    }
    else {
        $message = $_POST["message"];
    }
    if (!isset($_POST["spamcheck"]))  {
        $spamcheckErr = "Verify you are not spam.";
    }
    else {
        $spamcheck = $_POST["spamcheck"];
    }

}

?>

这是我的HTML:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <div id="contact_input" class="col1">
            <input name="name" placeholder="Name*" type="text" class="text" value="<?php echo htmlspecialchars($name);?>">
            <span class="error"><?php echo $nameErr;?></span><br />
            <input name="email" placeholder="Email*" type="email" class="text" value="<?php echo htmlspecialchars($email);?>">
            <span class="error"><?php echo $emailErr;?></span><br />
            <input name="phone" placeholder="Phone #" type="tel" class="text" value="<?php echo $phone;?>" />
        </div>
        <div id="contact_input" class="col2">
            <textarea name="message" placeholder="Message*" rows="10" cols="25"><?php echo $message?></textarea>
            <span class="error"><?php echo $messageErr;?></span>
        </div>
        <div id="contact_input" class="col3">
            <input id="spamcheck" type="checkbox" name="spamcheck" value="<?php echo htmlspecialchars($spamcheck);?>">I am human.*<br />
            <span class="error"><?php echo $spamcheckErr;?></span>
            <input id="submit" type="submit" name="submit" value="Send" class="button" /><br />
            <span>*Required Field.</span>
        </div>
    </form>

当字段为空时,我在每个字段下都会收到正确的错误消息,但我无法将其发送到我的电子邮件中。然而,每当我加载页面时它都会通过电子邮件发送给我,当我进行这些更改时,它停止提交。

作为新的联系表格与必填字段,我似乎无法在其他地方找到明确的答案。

我怀疑它与if (isset($_GET['submit']))有关。因为那是我做出改变并开始遇到问题的地方。

3 个答案:

答案 0 :(得分:1)

您必须在表单中的?submit字符串中添加action,否则$_GET['submit']将被取消设置。

<form method="post" action="?submit">

或者您可以更改isset函数以检查$_POST var而不是$_GET var

if (isset($_POST['submit'])) {

编辑:以下是您应该使用验证脚本

的内容
if (!empty($_POST['submit'])) {
    $error = array();
    if (empty($_POST['email'])) $error[] = 'Please enter your email';
    // and so on...

    if (empty($error)) {
        // Send email script goes here
    }
}

然后让您的用户在出现任何错误时显示:

if (!empty($error)) foreach ($error as $e) echo '<p class="error">'.$e.'</p>';

这使您可以轻松地添加更多错误消息,并使用数组的空属性来验证验证中是否存在错误。

答案 1 :(得分:0)

事实上,我并未低估if (isset($_GET['submit']))。它为什么存在?

$field1 = NULL;
 $field2 = NULL;

    if(isset($_POST["submit"])){
    $field1 = $_POST["field1"];
    $field2 = $_POST["field2"]; 
    //etc
    mail ("youremail", "yoursubject", "$field1 $field2 $field3 etc.");
        }

答案 2 :(得分:0)

我测试了您的代码并检查了所有内容,但此行除外:

if (isset($_GET['submit'])) {

只需要更改为:

if (isset($_POST['submit'])) {

问题实际上是使用$_GET代替$_POST


修改

添加了一些条件语句:

if (($_POST['name'] &&  $_POST['email'] &&  $_POST['message'] !="") 
&&  isset($_POST["spamcheck"]) !="")

完整代码(使用下面的完整版):

<?php   

if (isset($_POST['submit'])) {
    $body = '';

$body .= 'Name: ' . $_POST['name'] . "\n\n";
$body .= 'Phone: ' . $_POST['phone'] . "\n\n";
$body .= 'Email: ' . $_POST['email'] . "\n\n";
$body .= 'Message: ' . $_POST['message'] . "\n\n";

if (($_POST['name'] &&  $_POST['email'] &&  $_POST['message'] !="") &&  isset($_POST["spamcheck"]) !="")
    {
mail('myemailaddress@gmail.com', 'Contact Form', $body, 'From: no-reply@mycompany.com');
    }

}   

// define variables and initialize with empty values
$nameErr = $addressErr = $emailErr = $messageErr = $spamcheckErr = "";
$name = $address = $email = $message = $spamcheck = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {

        $nameErr = "Please enter your name.";
    }
    else {
        $name = $_POST["name"];
    }

    if (empty($_POST["email"])) {
        $emailErr = "Please enter your email."; 
    }
    else {
        $email = $_POST["email"];
    }

    if (empty($_POST["message"]))  {
        $messageErr = "Cannot leave message box blank."; 
    }
    else {
        $message = $_POST["message"];
    }
    if (!isset($_POST["spamcheck"]))  {
        $spamcheckErr = "Verify you are not spam.";
    }
    else {
        $spamcheck = $_POST["spamcheck"];
    }

}

?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <div id="contact_input" class="col1">
            <input name="name" placeholder="Name*" type="text" class="text" value="<?php echo htmlspecialchars($name);?>">
            <span class="error"><?php echo $nameErr;?></span><br />
            <input name="email" placeholder="Email*" type="email" class="text" value="<?php echo htmlspecialchars($email);?>">
            <span class="error"><?php echo $emailErr;?></span><br />
            <input name="phone" placeholder="Phone #" type="tel" class="text" value="<?php echo $phone;?>" />
        </div>
        <div id="contact_input" class="col2">
            <textarea name="message" placeholder="Message*" rows="10" cols="25"><?php echo $message?></textarea>
            <span class="error"><?php echo $messageErr;?></span>
        </div>
        <div id="contact_input" class="col3">
            <input id="spamcheck" type="checkbox" name="spamcheck" value="<?php echo htmlspecialchars($spamcheck);?>">I am human.*<br />
            <span class="error"><?php echo $spamcheckErr;?></span>
            <input id="submit" type="submit" name="submit" value="Send" class="button" /><br />
            <span>*Required Field.</span>
        </div>
</form>