这是我正在使用的表单。这不是我制作的东西,在引导中找到它并把它移了过来。它可以工作,但理想情况下,当访问者提交它时,它会将它们保持在同一页面上,并显示来自引导程序的javascript弹出窗口,说"谢谢,我们已收到您的信息,将与您联系!
当我尝试显示 Javascript 时(现在只使用基本警报),它看起来不像执行......相反,它只是将代码写入新窗口 Contact.php。
这是我正在使用的页面:
http://www.jackalopemedia.com/benefits
联系表格:
<?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 = 'mcollinsblog@gmail.com'; // Your Email
$message_min_length = 0; // 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->phone = trim($details['phone']);
$this->subject = 'Contact from Your Website'; // Subject
$this->message = $this->name . " " . $this->email . " " . 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>Please enter your name</p>';
$this->response_status = 0;
}
// Check email
if(!$this->email)
{
$this->response_html .= '<p>Please enter an e-mail address</p>';
$this->response_status = 0;
}
// Check valid email
if($this->email && !$this->validateEmail())
{
$this->response_html .= '<p>Please enter a valid e-mail address</p>';
$this->response_status = 0;
}
// Check message length
if(!$this->message || strlen($this->message) < $this->message_min_length)
{
$this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
$this->response_status = 0;
}
}
private function sendEmail(){
$mail = mail($this->email_admin, $this->subject, $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>Thank You!</p>';
echo '<script type="text/javascript">'
, 'alert(thank you);'
, '</script>';
}
}
function sendRequest(){
$this->validateFields();
if($this->response_status)
{
$this->sendEmail();
}
$response = array();
$response['status'] = $this->response_status;
$response['html'] = $this->response_html;
if ($response['status'] == '0') { // If error
echo json_encode($response);
} else {
echo $response['html'];
}
}
}
$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();
?>
感谢您的任何回复!
所以你已经构建了一个类似 API 接收器的东西。您需要做的是使用 jQuery 异步发布到联系人.php并将响应写入页面。
您可以像这样将一些JS添加到您的页面中:
$('#contact-form').on('submit', function(event) {
event.preventDefault(); //prevent the form from submitting
$.post('contact.php', $('#contact-form').serialize()) //send the request to contact.php
.done(function(response) {
$('body').append(response.html); //once the response is back, append it to the body tag
});
});
您需要使用 ajax 提交表单,然后将邮件函数的结果返回到将显示错误或成功消息的回调函数。