Php 已在emailer提交按钮上更新此代码
好的,在我读了你的第一篇博文之后,我根据我的理解对代码进行了更新(试图使它更简单),因为,同样,我是一个新手。现在回想起来,这可能就是问题所在。所以我感谢你的耐心;请不要杀我。。。以下是我在服务器上更改和测试的内容: 以下是实际的网页(ContactsUs.php):Php 已在emailer提交按钮上更新此代码,php,html-email,email-validation,Php,Html Email,Email Validation,好的,在我读了你的第一篇博文之后,我根据我的理解对代码进行了更新(试图使它更简单),因为,同样,我是一个新手。现在回想起来,这可能就是问题所在。所以我感谢你的耐心;请不要杀我。。。以下是我在服务器上更改和测试的内容: 以下是实际的网页(ContactsUs.php): 联系我们表格 $(文档).ready(函数(){ $(“#qForm”).validate({ 规则:{ 名字:“必选”, 姓氏:“必需”, 电邮:{ 要求:正确, 电子邮件:真的 }, 评论:“必需” }, 信息:{ 名字:“
联系我们表格
$(文档).ready(函数(){
$(“#qForm”).validate({
规则:{
名字:“必选”,
姓氏:“必需”,
电邮:{
要求:正确,
电子邮件:真的
},
评论:“必需”
},
信息:{
名字:“需要名字”,
姓氏:“需要姓氏”,
电邮:{
必填:“需要电子邮件”,
电子邮件:“无效的电子邮件地址”
},
评论:“你必须写一条信息”
}
});
});
联系我们表格
名字:
姓氏:
电邮:
评论:
然后我将操作文件(contact.php)更改为:
联系我们表格
$(文档).ready(函数(){
$(“#qForm”).validate({
规则:{
名字:“必选”,
姓氏:“必需”,
电邮:{
要求:正确,
电子邮件:真的
},
评论:“必需”
},
信息:{
名字:“需要名字”,
姓氏:“需要姓氏”,
电邮:{
必填:“需要电子邮件”,
电子邮件:“无效的电子邮件地址”
},
评论:“你必须写一条信息”
}
});
});
宠爱宠物的家。
快乐宠物提摩尼
我的主人带我去桑迪家洗澡,我得到了“spaw”治疗漫游者
保证满意
时间:星期一至五:上午六时至晚上十一时;;周六和周日:上午8点至晚上10点
指向其他本地服务的链接:
联系我们表格
名字:
姓氏:
电邮:
评论:
&抄袭;版权所有2015 Time Live,Inc.保留所有权利
根据您的建议,这里是新的post文件(ContactProcess.php):
<?php
if(isset($_POST['firstname'])) {
$contact = validate_inputs($_POST);
if(in_array(false, $contact) === true) {
echo process_errors($contact);
exit;
}
else {
/* Let's prepare the message for the e-mail */
ob_start();
?>Hello!
Your contact form has been submitted by:
First Name: <?php echo $contact['firstname']; ?>
Last Name: <?php echo $contact['lastname']; ?>
E-mail: <?php echo $contact['email']; ?>
Comments:
<?php echo $contact['comments']; ?>
End of message
<?php
$message = ob_get_contents();
ob_end_clean();
// Send the message here
if(send_email(array("to"=>"greatscott971@gmail.com","from"=>$contact['email'],"subject"=>$contact['subject'],"message"=>$contact['comments']))) {
header('Location: thanks.html');
exit();
}
else
die("An error occurred while sending. Please contact the administrator.");
}
}
尝试将一些逻辑拆分为小函数,这样更容易跟踪任务。同样对于表单,请尝试通过jQuery使用表单验证:
表单页面:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="/css/default.css" rel="stylesheet">
<title>Contact Us Form</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#qForm").validate({
rules: {
firstname: "required",
lastname: "required",
email: {
required: true,
email: true
},
comments: "required"
},
messages: {
firstname: "First Name Required",
lastname: "Last Name Required",
email: {
required: "Email Required",
email: "Invalid Email Address"
},
comments: "You must write a message"
}
});
});
</script>
</head>
<body>
<div id="wrapper">
<div id="header">
<div id="logo">
<h1 id="sitename"><img src="Images/logo.jpg" alt="logo" width="270" height="105" /></span></h1>
<h2 class="description">The home for pampered pets.</h2>
</div>
<div id="headercontent">
<h2>Happy Pets-timonials</h2>
<p>My owner took me to Sandy's for a bath and I got the 'spaw' treatment. - Rover</p>
</div>
<div id="sitecaption"> Satisfaction <span class="bigger">Guaranteed</span> </div>
</div>
<div id="ripmain">
<div id="menuet">
<nav>
<ul id="menubar">
<li><a href="PSTP.html">Home</a></li>
<li><a href="AboutUs.html">About</a></li>
<li><a href="Location.html">Location</a></li>
<li><a href="GroomingServices.html">Grooming</a></li>
<li><a href="ContactUs.html">Contact Us</a></li>
</ul>
</nav>
</div>
<form method="POST" action="contact.php" id="qForm">
<fieldset>
<legend>Contact Us Form</legend>
<p>First Name: <input type="text" size="32" name="firstname" /></p>
<p>Last Name: <input type="text" size="32" name="lastname" /></p>
<p>Email: <input type="text" size="32" id="email" name="email" /></p>
<div id="rsp_email"><!-- --></div>
<td>Comments: </td>
<td>
<textarea name="comments" cols="40" rows="3" wrap="virtual"></textarea>
</td>
<input type="hidden" name="subject" value="online_submission" />
<p><input type="submit" value="Submit"></p>
</fieldset>
</form>
<div id="footer"> © Copyright 2015 Time Live, Inc. All rights reserved. <br>
Hours: Mon-Fri: 6 am to 11 pm; Sat & Sun: 8 am to 10pm <br>
Links to other local services: <li><a href="http://www.hillsidevetclinic.org">Hillside Vet Clinic</a></li> <li><a href="http://www.petsmart.com">PetSmart Stores</a></li> <li><a href="http://www.poochhotel.com">Pooch Hotel</a> </div>
</body>
</html>
// This will return error messages (you could expand it to be database driven)
function error_codes($code = false)
{
$valid['firstname'] = "Enter your name";
$valid['lastname'] = "Enter your name";
$valid['subject'] = "Write a subject";
$valid['email'] = "Invalid email";
$valid['comments'] = "Write your comments";
return (isset($valid[$code]))? $valid[$code] : false;
}
// Run the validation and return populated array
function validate_inputs($REQUEST)
{
/* Check all form inputs using check_input function */
$valid['firstname'] = check_input($REQUEST['firstname']);
$valid['lastname'] = check_input($REQUEST['lastname']);
$valid['subject'] = check_input($REQUEST['subject']);
$valid['email'] = check_input($REQUEST['email'],"email");
$valid['comments'] = check_input($REQUEST['comments']);
return $valid;
}
// Modify your validate function a bit to do only validation, no returning of errors
function check_input($data = false, $type = false)
{
if($type == 'email')
return (filter_var($data,FILTER_VALIDATE_EMAIL))? $data : false;
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return (!empty($data))? $data : false;
}
// This will loop through returned values and populate errors based on empty
function process_errors($array = false)
{
if(!is_array($array))
return $array;
foreach($array as $key => $value) {
if(empty($value))
$errors[] = error_codes($key);
}
return (!empty($errors))? show_error($errors) : false;
}
// display errors via buffer output
function show_error($myError)
{
ob_start();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<b>Please correct the following error:</b><br />
<?php echo implode("<br />".PHP_EOL,$myError); ?>
</body>
</html>
<?php
$data = ob_get_contents();
ob_end_clean();
return $data;
}
function send_email($settings = false)
{
$to = (!empty($settings['to']))? $settings['to']:false;
$from = (!empty($settings['from']))? "From:".$settings['from'].PHP_EOL:false;
$subject = (!empty($settings['subject']))? $settings['subject']:false;
$message = (!empty($settings['message']))? $settings['message']:false;
if(in_array(false, $settings) === true)
return false;
return (mail($to,$subject,$message));
}
// Include above functions
if(isset($_POST['firstname'])) {
$contact = validate_inputs($_POST);
if(in_array(false, $contact) === true) {
echo process_errors($contact);
exit;
}
else {
/* Let's prepare the message for the e-mail */
ob_start();
?>Hello!
Your contact form has been submitted by:
First Name: <?php echo $contact['firstname']; ?>
Last Name: <?php echo $contact['lastname']; ?>
E-mail: <?php echo $contact['email']; ?>
Comments:
<?php echo $contact['comments']; ?>
End of message
<?php
$message = ob_get_contents();
ob_end_clean();
// Send the message here
if(send_email(array("to"=>"greatscott971@gmail.com","from"=>$contact['email'],"subject"=>$contact['subject'],"message"=>$contact['comments']))) {
header('Location: thanks.html');
exit();
}
else
die("An error occurred while sending. Please contact the administrator.");
}
}
联系我们表格
$(文档).ready(函数(){
$(“#qForm”).validate({
规则:{
名字:“必选”,
姓氏:“必需”,
电邮:{
要求:正确,
电子邮件:真的
},
评论:“必需”
},
信息:{
名字:“需要名字”,
姓氏:“需要姓氏”,
电邮:{
必填:“需要电子邮件”,
电子邮件:“无效的电子邮件地址”
},
评论:“你必须写一条信息”
}
});
});
宠爱宠物的家。
快乐宠物提摩尼
我的主人带我去桑迪家洗澡,我得到了“spaw”治疗漫游者
保证满意
时间:星期一至五:上午六时至晚上十一时;;周六和周日:上午8点至晚上10点
指向其他本地服务的链接:
联系我们表格
名字:
姓氏:
电邮:
评论:
&抄袭;版权所有2015 Time Live,Inc.保留所有权利
联系人表单上需要的功能:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="/css/default.css" rel="stylesheet">
<title>Contact Us Form</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#qForm").validate({
rules: {
firstname: "required",
lastname: "required",
email: {
required: true,
email: true
},
comments: "required"
},
messages: {
firstname: "First Name Required",
lastname: "Last Name Required",
email: {
required: "Email Required",
email: "Invalid Email Address"
},
comments: "You must write a message"
}
});
});
</script>
</head>
<body>
<div id="wrapper">
<div id="header">
<div id="logo">
<h1 id="sitename"><img src="Images/logo.jpg" alt="logo" width="270" height="105" /></span></h1>
<h2 class="description">The home for pampered pets.</h2>
</div>
<div id="headercontent">
<h2>Happy Pets-timonials</h2>
<p>My owner took me to Sandy's for a bath and I got the 'spaw' treatment. - Rover</p>
</div>
<div id="sitecaption"> Satisfaction <span class="bigger">Guaranteed</span> </div>
</div>
<div id="ripmain">
<div id="menuet">
<nav>
<ul id="menubar">
<li><a href="PSTP.html">Home</a></li>
<li><a href="AboutUs.html">About</a></li>
<li><a href="Location.html">Location</a></li>
<li><a href="GroomingServices.html">Grooming</a></li>
<li><a href="ContactUs.html">Contact Us</a></li>
</ul>
</nav>
</div>
<form method="POST" action="contact.php" id="qForm">
<fieldset>
<legend>Contact Us Form</legend>
<p>First Name: <input type="text" size="32" name="firstname" /></p>
<p>Last Name: <input type="text" size="32" name="lastname" /></p>
<p>Email: <input type="text" size="32" id="email" name="email" /></p>
<div id="rsp_email"><!-- --></div>
<td>Comments: </td>
<td>
<textarea name="comments" cols="40" rows="3" wrap="virtual"></textarea>
</td>
<input type="hidden" name="subject" value="online_submission" />
<p><input type="submit" value="Submit"></p>
</fieldset>
</form>
<div id="footer"> © Copyright 2015 Time Live, Inc. All rights reserved. <br>
Hours: Mon-Fri: 6 am to 11 pm; Sat & Sun: 8 am to 10pm <br>
Links to other local services: <li><a href="http://www.hillsidevetclinic.org">Hillside Vet Clinic</a></li> <li><a href="http://www.petsmart.com">PetSmart Stores</a></li> <li><a href="http://www.poochhotel.com">Pooch Hotel</a> </div>
</body>
</html>
// This will return error messages (you could expand it to be database driven)
function error_codes($code = false)
{
$valid['firstname'] = "Enter your name";
$valid['lastname'] = "Enter your name";
$valid['subject'] = "Write a subject";
$valid['email'] = "Invalid email";
$valid['comments'] = "Write your comments";
return (isset($valid[$code]))? $valid[$code] : false;
}
// Run the validation and return populated array
function validate_inputs($REQUEST)
{
/* Check all form inputs using check_input function */
$valid['firstname'] = check_input($REQUEST['firstname']);
$valid['lastname'] = check_input($REQUEST['lastname']);
$valid['subject'] = check_input($REQUEST['subject']);
$valid['email'] = check_input($REQUEST['email'],"email");
$valid['comments'] = check_input($REQUEST['comments']);
return $valid;
}
// Modify your validate function a bit to do only validation, no returning of errors
function check_input($data = false, $type = false)
{
if($type == 'email')
return (filter_var($data,FILTER_VALIDATE_EMAIL))? $data : false;
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return (!empty($data))? $data : false;
}
// This will loop through returned values and populate errors based on empty
function process_errors($array = false)
{
if(!is_array($array))
return $array;
foreach($array as $key => $value) {
if(empty($value))
$errors[] = error_codes($key);
}
return (!empty($errors))? show_error($errors) : false;
}
// display errors via buffer output
function show_error($myError)
{
ob_start();
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<b>Please correct the following error:</b><br />
<?php echo implode("<br />".PHP_EOL,$myError); ?>
</body>
</html>
<?php
$data = ob_get_contents();
ob_end_clean();
return $data;
}
function send_email($settings = false)
{
$to = (!empty($settings['to']))? $settings['to']:false;
$from = (!empty($settings['from']))? "From:".$settings['from'].PHP_EOL:false;
$subject = (!empty($settings['subject']))? $settings['subject']:false;
$message = (!empty($settings['message']))? $settings['message']:false;
if(in_array(false, $settings) === true)
return false;
return (mail($to,$subject,$message));
}
// Include above functions
if(isset($_POST['firstname'])) {
$contact = validate_inputs($_POST);
if(in_array(false, $contact) === true) {
echo process_errors($contact);
exit;
}
else {
/* Let's prepare the message for the e-mail */
ob_start();
?>Hello!
Your contact form has been submitted by:
First Name: <?php echo $contact['firstname']; ?>
Last Name: <?php echo $contact['lastname']; ?>
E-mail: <?php echo $contact['email']; ?>
Comments:
<?php echo $contact['comments']; ?>
End of message
<?php
$message = ob_get_contents();
ob_end_clean();
// Send the message here
if(send_email(array("to"=>"greatscott971@gmail.com","from"=>$contact['email'],"subject"=>$contact['subject'],"message"=>$contact['comments']))) {
header('Location: thanks.html');
exit();
}
else
die("An error occurred while sending. Please contact the administrator.");
}
}
//这将返回错误消息(您可以将其扩展为数据库驱动)
函数错误\u代码($code=false)
{
$valid['firstname']=“输入您的姓名”;
$valid['lastname']=“输入您的姓名”;
$valid['subject']=“编写主题”;
$valid['email']=“无效电子邮件”;
$valid['comments']=“写下你的评论”;
返回(isset($valid[$code])?$valid[$code]:false;
}
//运行验证并返回填充的数组
函数验证输入($REQUEST)
{
/*使用检查输入功能检查所有表单输入*/
$valid['firstname']=检查输入($REQUEST['firstname']);
$valid['lastname']=检查输入($REQUEST['lastname']);
$valid['subject']=检查输入($REQUEST['subject']);
$valid['email']=检查输入($REQUEST['email'],“email”);
$valid['comments']=检查输入($REQUEST['comments']);
返回$valid;
}
//稍微修改一下验证函数,只进行验证,不返回错误
函数检查\输入($data=false,$type=false)
{
如果($type=='email')
返回(filter_var($data,filter_VALIDATE_EMAIL))?$data:false;
$data=修剪($data);
$data=条带斜杠($data);
$data=htmlspecialchars($data);
返回(!empty($data))?$data:false;
}
//这将循环返回值,并根据空值填充错误
函数进程错误($array=false)
{
如果(!is_数组($array))
返回$array;
foreach($key=>$value的数组){
if(空($value))
$errors[]=错误代码($key);
}
返回(!empty($errors))?显示错误($errors):false;
}
//通过缓冲区输出显示错误
函数显示错误($myError)
{
ob_start();
?>
无标题文件
请更正以下错误:
Thx!我感谢您的帮助。好的;查看了contact.php文件,没有发现可能存在的问题;正文中有一条消息说请