doupi1532 2016-08-22 00:25
浏览 26

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

I am trying to create a form that will validate through php before submitting it to email, once the submit button has been pressed. I have been fooling around with the code and I am not skilled enough to figure it out. Currently, it will send the email whether the form has validated or not.

<?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 . "

" . "Last Name:" . " " . $last_name . "

" . "Phone#:" . " " . $area_code . " " . $phone . "

" . "Email:" . " " . $_POST['email'] . "

" . "Courthouse" . " " . $courthouse . "

" . "Case Number:" . " " . $case_number . "

" . "wrote the following:" . "

" . $_POST['message'];
    $message2 = ""  . "

" .  ""   . "
" . "Phone # - " . "
" . "Fax # - " . "
" . "" . "
" . "";

    $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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 seatunnel 怎么配置Elasticsearch
    • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
    • ¥15 (标签-MATLAB|关键词-多址)
    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序