0

这是我正在使用的表格。这不是我做的,在引导程序中找到它并将它移到了过去。它可以工作,但理想情况下,当访问者提交它时,它会将它们保持在同一页面上并显示引导程序中的 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();

?>

感谢您的任何回复!

4

2 回答 2

0

所以你已经构建了一个类似于 API 接收器的东西。您需要做的是使用 jQuery 异步发布到 contact.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
      });
 });
于 2013-10-26T21:03:54.543 回答
0

您需要使用 ajax 提交表单,然后将邮件函数的结果返回给将显示错误或成功消息的回调函数。

于 2013-10-26T20:58:46.167 回答