我使用了一些我从我的网站发送联系表单的php。
HTML看起来像这样:
<div id="wrap">
<div id='form_wrap'>
<form id="contact-form" action="javascript:alert('success!');">
<p id="formstatus"></p>
<input type="text" name="name" value="" id="name" placeholder="Voornaam" required/>
<input type="text" name="email" value="" id="email" placeholder="E-mail adres" required/>
<textarea name="message" value="Your Message" id="message" placeholder="Uw vraag of projectomschrijving" required></textarea>
<input type="submit" name ="submit" value="Offerte aanvragen" />
</form>
</div>
</div>
PHP看起来像这样:
<?php
define("WEBMASTER_EMAIL", 'your@emailadress.com');
error_reporting (E_ALL ^ E_NOTICE);
function ValidateEmail($email)
{
$regex = '/([a-z0-9_.-]+)'. # name
'@'. # at
'([a-z0-9.-]+){2,255}'. # domain & possibly subdomains
'.'. # period
'([a-z]+){2,10}/i'; # domain extension
if($email == '')
return false;
else
$eregi = preg_replace($regex, '', $email);
return empty($eregi) ? true : false;
}
$post = (!empty($_POST)) ? true : false;
if($post)
{
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = stripslashes($_POST['subject']);
$message = stripslashes($_POST['message']);
$error = '';
// Check name
if(!$name || $name == "Name*")
$error .= 'Please enter your name.<br />';
// Check email
if(!$email || $email == "Email*")
$error .= 'Please enter an e-mail address.<br />';
if($email && !ValidateEmail($email))
$error .= 'Please enter a valid e-mail address.<br />';
// Check message
if(!$message)
$error .= "Please enter your message. <br />";
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
echo 'OK';
}
else
echo '<div class="formstatuserror">'.$error.'</div>';
}?>
效果很好!但是我需要在我的表单中添加一些INPUTS。我知道如何将这些添加到HTML。例如,我添加了这个:
<input type="text" name="lastname" value="" id="lastname" placeholder="Achternaam" required/>
但我无法找到将此输入添加到我在邮箱中收到的电子邮件的好方法...我在PHP中添加哪些内容?我尝试了很多东西......
希望你们能帮助我!
大卫
答案 0 :(得分:1)
在以下几行之后:
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = stripslashes($_POST['subject']);
$message = stripslashes($_POST['message']);
您实例化包含字段值(lastname)的变量:
$lastname = stripslashes($_POST['lastname']);
然后验证输入是否为空或其他:
// Check message
if(!$lastname )
$error .= "Please enter your lastname. <br />";
最后,您使用变量姓氏在电子邮件中显示它:
"From: ".$name." ".$lasname." <".$email.">\r\n"
Etvoilà!
编辑:如果你想在邮件上使用这个输入,你有$message
变量,你可以这样做:
if(!$error)
{
$message .= "<p>Message sent by $lastname";
...
答案 1 :(得分:0)
如果您不想将其放入电子邮件标题中作为“来自”的补充。或者&#39;主题&#39;,您还可以继续将信息附加到电子邮件正文或您的$消息。例如:
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = stripslashes($_POST['subject']);
$message = "Last Name: " . stripslashes($_POST['lastname']) . "\r\n";
$message .= "Message: " . stripslashes($_POST['message']);
其他选项也有效,它实际上只取决于&#39;在电子邮件中,您希望获得此额外信息。