我有一个带有验证的联系表(如果我有一个可选字段),如果用户确实填写了,我希望将其通过电子邮件发送出去。这样简单地扔进去就可以了吗?
$found = $_POST['found'];
这是我的表格:
<?php
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
// validate the variables ======================================================
// if any of these variables don't exist, add an error to our $errors array
if (empty($_POST['firstName']))
$errors['firstName'] = 'First Name is required.';
if (empty($_POST['lastName']))
$errors['lastName'] = 'Last Name is required.';
if (empty($_POST['companyName']))
$errors['companyName'] = 'Company Name is required.';
if (empty($_POST['companyAddress']))
$errors['companyAddress'] = 'Company Address is required.';
if (empty($_POST['city']))
$errors['city'] = 'City is required.';
if (empty($_POST['state']))
$errors['state'] = 'State is required.';
if (empty($_POST['emailAddress'])) {
$errors['emailAddress'] = 'Email Address is required.';
} else {
$emailAddress = filter_var($errors['emailAddress'], FILTER_SANITIZE_EMAIL);
if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
$errors['emailAddress'] = 'Email Address is invalid.';
}
}
if (empty($_POST['comment']))
$errors['comment'] = 'Comment is required.';
if (empty($_POST['g-recaptcha-response']))
$errors['recaptcha'] = 'Captcha is required.';
// return a response ===========================================================
// Check ReCaptcha Validation
if(!validateRecaptcha($secret, $_POST['g-recaptcha-response'], $_SERVER["REMOTE_ADDR"]))
{
$errors['recaptcha'] = 'Captcha is required.';
}
if ( ! empty($errors)) {
// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;
} else {
// if there are no errors process our form, then return a message
// DO ALL YOUR FORM PROCESSING HERE
// THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)
// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
}
// return all our data to an AJAX call
if ($errors) {
$data['success'] = false;
$data['errors'] = $errors;
} else {
//Begin of send email
?>
还是有另一种更合适的方法来实现这一目标?
我的目标是保持组织和风格不变。
该脚本执行服务器端验证,并用javascript打印出所有错误,并根据反馈对表单进行必要的CSS / HTML更改。显然,这使用JSON / AJAX来传达错误。
“已找到”字段是一个可选字段,与其余字段不同。