如何阻止表单发送空数据或错误数据?

时间:2014-03-05 23:52:23

标签: php forms

我刚刚完成构建此表单并且运行得非常好,这得益于stackoverflow的用户社区;但我现在已经注意到我的表单在不同情况下测试时出现问题。

主要问题是无论用户是否输入了正确的数据,它都会发送信息,无论信息是否存在。错误显示在页面上,但即使页面上显示错误,也会发送数据。

我想了解如何在字段为空或输入错误内容时停止发送电子邮件表单?

以下是我正在使用的PHP代码:

<?php

function test_input($data){

    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);

    return $data;
}

function clean_string($string) {

    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);

}


// define variables and set to empty values
$first_nameErr = $last_nameErr = $emailErr = $overviewErr = "";
$first_name = $last_name = $email = $overview = "";

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

$email_to = "myself@mydomain.com";
$email_subject = "Contact us - My company's name";
{

if (empty($_POST["first_name"]))
{$first_nameErr = "(First Name is required)";}

else
{$first_name = test_input($_POST["first_name"]);

// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name))
      {
      $first_name = "(Only letters and white space allowed)"; 
      }
    }

if (empty($_POST["last_name"]))
{$last_nameErr = "(Last Name is required)";}

else
    {$last_name = test_input($_POST["last_name"]);

// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name))
      {
      $last_name = "(Only letters and white space allowed)"; 
      }
    }

if (empty($_POST["email"]))
    {$emailErr = "(Email ID is required)";}

else
    {$email = test_input($_POST["email"]);

// check if e-mail address syntax is valid
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
      {
      $emailErr = "(Invalid email format)"; 
      }
    }

if (empty($_POST["overview"]))
    {$overviewErr = "(Overview is required)";}

else
    {$overview = test_input($_POST["overview"]);

// check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$overview))
      {
      $overview = "(Only letters and white space allowed)"; 
      }
    }

}

//Email & SEND INFO
$email_message = "Form details below.\n\n";

    $email_message .= "First Name: ".clean_string($first_name)."\n";

    $email_message .= "Last Name: ".clean_string($last_name)."\n";

    $email_message .= "Email: ".clean_string($email)."\n";

    $email_message .= "Services: ".clean_string(implode(', ', $service))."\n";

    $email_message .= "Overview: ".clean_string($overview)."\n";





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- Success HTML -->

Thank you for contacting us. We will be in touch with you very soon.

<?php

}

?>

2 个答案:

答案 0 :(得分:0)

如果表单中没有任何内容可以发送,我相信你想要阻止表单发送任何数据,对吧?如果是这样,那么你需要通过javascript来做到这一点。最简单的方法是使用jquery。我建议This tutorial。这将有助于你开始

答案 1 :(得分:0)

我认为你的代码块可能不正确试试这个:

<?php

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
}

// define variables and set to empty values
$first_nameErr = $last_nameErr = $emailErr = $overviewErr = "";
$first_name = $last_name = $email = $overview = "";

if(isset($_POST['email'])) {
    $email_to = "myself@mydomain.com";
    $email_subject = "Contact us - My company's name";
}

if (empty($_POST["first_name"])) {
    $first_nameErr = "(First Name is required)";
} else {
    $first_name = test_input($_POST["first_name"]);
}

// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
      $first_name = "(Only letters and white space allowed)"; 
}

if (empty($_POST["last_name"])) {
   $last_nameErr = "(Last Name is required)";
} else {
   $last_name = test_input($_POST["last_name"]);
}

// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
    $last_name = "(Only letters and white space allowed)"; 
}

if (empty($_POST["email"])) {
    $emailErr = "(Email ID is required)";
} else {
    $email = test_input($_POST["email"]);
}

// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
    $emailErr = "(Invalid email format)"; 
}

if (empty($_POST["overview"])) {
    $overviewErr = "(Overview is required)";
} else {
    $overview = test_input($_POST["overview"]);
}

// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$overview)) {
  $overview = "(Only letters and white space allowed)"; 
}


//Email & SEND INFO
$email_message = "Form details below.\n\n";
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Services: ".clean_string(implode(', ', $service))."\n";
$email_message .= "Overview: ".clean_string($overview)."\n";


// create email headers 
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($email_to, $email_subject, $email_message, $headers);  


if ($first_nameErr || $last_nameErr || $emailErr || $overviewErr) {
    echo "Thank you for contacting us. We will be in touch with you very soon.";
} else {
    echo "There are some errors in your form: " . $first_nameErr . ', ' . $last_nameErr . ', ' . $emailErr . ', ' . $overviewErr;
}

不确定是否会修复所有内容,但应该更容易看到发生了什么。