【发布时间】:2017-08-12 04:11:34
【问题描述】:
这是我到目前为止得到的。我的内联代码在哪里?我也有表单被编码的 html 页面,但我将它链接到这个 php 页面。表单效果很好,只是想为客户添加一些样式。
<?php
// define variables and set to empty values
$name_error = $email_error = $phone_error = $subject_error = "";
$name = $email = $phone = $message = $subject = $success = "";
//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$name_error = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$name_error = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$email_error = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = "Invalid email format";
}
}
if (empty($_POST["phone"])) {
$phone_error = "Phone is required";
} else {
$phone = test_input($_POST["phone"]);
// check if phone number is well-formed
if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3} [\s-]?\d{4}$/i",$phone)) {
$phone_error = "Invalid phone number";
}
}
if (empty($_POST["subject"])) {
$subject_error = "Subject is required";
} else {
$subject = test_input($_POST["subject"]);
// check if subject is proper text form
if (!preg_match("/^[a-zA-Z ]*$/",$subject)) {
$subject_error = "Invalid URL";
}
}
if (empty($_POST["message"])) {
$message = "";
} else {
$message = test_input($_POST["message"]);
}
if ($name_error == '' and $email_error == '' and $phone_error == '' and $subject_error == '' ){
$message_body = '';
unset($_POST['submit']);
foreach ($_POST as $key => $value){
$message_body .= "$key: $value\n";
}
// sends message to an email address specified.
$to = 'email@gmail.com';
$subject = 'Contact Form Submit';
if (mail($to, $subject, $name, $message)){
$success = "Message sent, thank you for contacting us!";
$name = $email = $phone = $message = $subject = '';
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
任何帮助将不胜感激!
【问题讨论】:
标签: php html forms styles inline