【发布时间】:2014-09-15 19:00:30
【问题描述】:
我下载了一个很棒的网站模板,除了联系表格外,一切正常。没有更多对模板的支持,所以现在我来了。
该表单确实向我发送了包含所有数据的电子邮件,但这些消息无法正常工作。
- 当数据填写错误时,您会收到错误填写的消息。但是当表格填写正确时,该消息不会被删除。
- 发送电子邮件时,没有显示已发送的消息。
- 发送电子邮件时,表单不会重置。
我找不到解决办法...
页面上的 HTML。
<form id="contact-form" class="contact-form" action="#">
<p class="contact-name">
<input id="contact_name" type="text" placeholder="Volledige naam" value="" name="name" />
</p>
<p class="contact-email">
<input id="contact_email" type="text" placeholder="E-mail adres" value="" name="email" />
</p>
<p class="contact-list">
<select id="contact_list" size="1" name="reason">
<option value="1" selected="selected">Kies de reden voor contact</option>
<option value="Informatie aanvraag">Informatie aanvraag</option>
<option value="Offerte aanvraag">Offerte aanvraag</option>
<option value="Anders">Andere reden</option>
</select>
</p>
<p class="contact-message">
<textarea id="contact_message" placeholder="Uw bericht" name="message" rows="15" cols="40"></textarea>
</p>
<p class="contact-submit">
<a id="contact-submit" class="submit" href="#">Verstuur uw e-mail</a>
</p>
<div id="response">
</div>
</form>
</div>
PHP 文件 (contact.php)
<?php
/*
* Contact Form Class
*/
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
$admin_email = 'info@fake.nl'; // Your Email
$message_min_length = 5; // Min Message Length
class Contact_Form{
function __construct($details, $email_admin, $message_min_length){
$this->name = stripslashes($details['name']);
$this->email = trim($details['email']);
$this->reason = trim($details['reason']);
$this->subject = 'Contact via de website'; // Subject
$this->message = stripslashes($details['message']);
$this->email_admin = $email_admin;
$this->message_min_length = $message_min_length;
$this->response_status = 1;
$this->response_html = '';
}
private function validateEmail(){
$regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';
if($this->email == '') {
return false;
} else {
$string = preg_replace($regex, '', $this->email);
}
return empty($string) ? true : false;
}
private function validateFields(){
// Check name
if(!$this->name)
{
$this->response_html .= '<p>Voer een naam in</p>';
$this->response_status = 0;
}
// Check email
if(!$this->email)
{
$this->response_html .= '<p>Voer een e-mail adres in</p>';
$this->response_status = 0;
}
// Check valid email
if($this->email && !$this->validateEmail())
{
$this->response_html .= '<p>Voer een geldig e-mail adres in</p>';
$this->response_status = 0;
}
// Check valid choice
if($this->reason == '1')
{
$this->response_html .= '<p>Maak een geldige keuze</p>';
$this->response_status = 0;
}
// Check message length
if(!$this->message || strlen($this->message) < $this->message_min_length)
{
$this->response_html .= '<p>Voer een bericht in. Het zou minstens '.$this->message_min_length.' tekens moeten hebben.</p>';
$this->response_status = 0;
}
}
private function sendEmail(){
$mail = mail($this->email_admin, $this->subject, $this->reason, $this->message,
"From: ".$this->name." <".$this->email.">\r\n"
."Reply-To: ".$this->email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
$this->response_status = 1;
$this->response_html = '<p>Bedankt voor uw bericht.</p>';
}
}
function sendRequest(){
$this->validateFields();
if($this->response_status)
{
$this->sendEmail();
}
$response = array();
$response['status'] = $this->response_status;
$response['html'] = $this->response_html;
echo json_encode($response);
}
}
$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();
?>
【问题讨论】:
标签: php json contact-form