希望PHP在将表单发送到电子邮件之前进行验证

时间:2016-08-22 00:25:31

标签: php forms validation

我正在尝试创建一个表单,一旦按下提交按钮,将在通过php验证之前将其提交到电子邮件。我一直在愚弄代码,我不够熟练,无法弄明白。目前,无论表格是否经过验证,它都会发送电子邮件。

<?php
// define variables and set to empty values
$first_nameErr = $emailErr = $last_nameErr = $phone = $area_code = "";
$first_name = $email = $last_name = $message = $phone = $area_code = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    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_nameErr = "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 e-mail address is well-formed
        if (!preg_match("/^[a-zA-Z-']*$/",$last_name)) {
            $last_nameErr = "Only letters and white space allowed"; 
        }
    }

    if (empty($_POST["email"])) {
        $emailErr = "Email is required";
    } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid email format"; 
        }
    }

    if (empty($_POST["area_code"])) {
        $area_codeErr = "Area Code is required";
    } else {
        $area_code = test_input($_POST["area_code"]);
        // check if e-mail address is well-formed
        if (!preg_match("/^[0-9'-]*$/",$area_code)) {
            $area_codeErr = "Only numbers allowed";
        }
    }

    if (empty($_POST["phone"])) {
        $phoneErr = "Phone is required";
    } else {
        $phone = test_input($_POST["phone"]);
        // check if e-mail address is well-formed
        if (!preg_match("/^[0-9]*$/",$phone)) {
            $phoneErr = "Only numbers and dashes allowed"; 
        }
    }

    if (empty($_POST["message"])) {
        $messageErr = "Brief Description is required";
    } else {
        $message = test_input($_POST["message"]);
    }
}
function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

if(isset($_POST['submit'])){
    ob_start();
    $to = "xyou1018@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $last_name = $_POST['last_name'];
    $phone = $_POST['phone'];
    $area_code = $_POST['area_code'];
    $case_number = $_POST['case_number'];
    $courthouse = $_POST['courthouse'];
    $subject = "Strobach Law Firm, LLC. Form Submission";
    $subject2 = "Strobach Law Firm, LLC. Form Received";
    $message = "First Name:" . " " . $first_name . "\n\n" . "Last Name:" . " " . $last_name . "\n\n" . "Phone#:" . " " . $area_code . " " . $phone . "\n\n" . "Email:" . " " . $_POST['email'] . "\n\n" . "Courthouse" . " " . $courthouse . "\n\n" . "Case Number:" . " " . $case_number . "\n\n" . "wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = ""  . "\n\n" .  ""   . "\n" . "Phone # - " . "\n" . "Fax # - " . "\n" . "" . "\n" . "";

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    header('Location: ');
}
?>
<!DOCTYPE html>
<head>
<title>Contact
</title>
</head>

<body class="home">
<h1><u>Contact Information</u></h1>
<p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<h5>An * denotes a required field</h5>
*First name:
<br><input type="text" name="first_name" Placeholder="First Name" value="<?php echo $first_name;?>" />
<span class="error"><?php echo $first_nameErr;?></span>
<p>
*Last name:
<br><input type="text" name="last_name" Placeholder="Last Name" size="20"   />
<span class="error"><?php echo $last_nameErr;?></span>
<p>
*Phone:
<br>
(<input type="text" name="area_code" Placeholder="xxx" size="1"  />) <span class="error"><?php echo $area_codeErr;?></span> <input type="text" name="phone" Placeholder="xxxxxxx" size="7" />
<span class="error"><?php echo $phoneErr;?></span>
<p>
*Email:
<br><input type="" name="email" Placeholder="Email@Email.com" size="20" />
<span class="error"><?php echo $emailErr;?></span>
<p>
Courthouse:
<br><input type="text" name="courthouse" Placeholder="Courthouse" size="20" />
<p>
Case Number:
<br><input type="text" name="case_number" Placeholder="Case Number" size="20" />
<p>
*Brief Description:
<br>
<textarea name="message" Cols="40" rows="20"/></textarea>
<span class="error"><?php echo $messageErr;?></span>
<p>
<input type="submit" name="submit" value="Submit">
</form>
If you prefer to use a different method to Email us, Please choose from the following below:
<p>
In Email Correspondence please include:
<p>
Name
<br>
Phone Number(s)
<br>
Your Case number, if you know it
<br>
What courthouse your case is located in
<br>
A brief description of your case
<p>
<a href="https://mail.google.com"><img src="img/gmail.jpg" alt="Gmail" height="21" width="28"></a> - Opens Gmail
<p>
<a href="https://mail.yahoo.com"><img src="img/ymail.jpg" alt="Ymail" height="25" width="24"></a> - Opens Yahoo Mail
<p>
<a href="https://webmail.aol.com"><img src="img/aolmail.jpg" alt="AOL Mail" height="25" width="25"></a> - Opens AOL Mail
<p>
<a href="https://mail.live.com"><img src="img/livemail.jpg" alt="Live Mail" height="21" width="32"></a> - Opens Live Mail
</body>
</html>

0 个答案:

没有答案