PHP表单:仅填写字段的成功电子邮件 [英] PHP form: Success email with only filled fields
本文介绍了PHP表单:仅填写字段的成功电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
过去为此苦苦挣扎了一段时间.基本上,我有6个字段,分别是姓名,电子邮件,电话,位置,日期和预算.名称和电子邮件是唯一必填字段.收到电子邮件后,我还会看到其余字段.可以仅接收已填写的字段吗?
Been struggling with this for some time now. Basically I have 6 fields, name, email, phone, location, date and budget. Name and email are the only required fields. When I get the email I see rest of the fields as well. Is it possible to receive only fields that have been filled?
这是代码;
<?php
// get posted data into local variables
$EmailFrom = "email@email.com";
$EmailTo = "email@email.com";
$Subject = "My form";
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Phone = Trim(stripslashes($_POST['Phone']));
$Location = Trim(stripslashes($_POST['Location']));
$Date = Trim(stripslashes($_POST['Date']));
$Budget = Trim(stripslashes($_POST['Budget']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=#\">";
exit;
}
$userip = ($_SERVER['X_FORWARDED_FOR']) ? $_SERVER['X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
// prepare email body text
$Body = "";
$Body .= "Location: "; $Body .= $Location; $Body .= "\n";
$Body .= "Date: "; $Body .= $Date; $Body .= "\n";
$Body .= "Budget: "; $Body .= $Budget; $Body .= "\n";
$Body .= "Name: "; $Body .= $Name; $Body .= "\n";
$Body .= "Email: "; $Body .= $Email; $Body .= "\n";
$Body .= "Phone: "; $Body .= $Phone; $Body .= "\n";
$Body .= "User's IP: ". $userip;
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=#\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=#\">";
}
?>
推荐答案
使用空检查值是否为是否发布:
Use empty to check if a value was posted or not:
$Phone = !empty($_POST['Phone']) ? trim(stripslashes($_POST['Phone'])) : false;
然后,如果未填写,随后的 $ Phone
将为false,您可以执行以下操作:
Then later $Phone
will be false if it wasn't filled in and you can do this:
if($Phone)
$Body .= "Phone: "; $Body .= $Phone; $Body .= "\n";
这篇关于PHP表单:仅填写字段的成功电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文