在PHP联系人表单中调用Javascript

在PHP联系人表单中调用Javascript,javascript,php,twitter-bootstrap,Javascript,Php,Twitter Bootstrap,这是我正在使用的表单。这不是我做的东西,是在引导程序中找到的,然后把它移了过来。它可以工作,但理想情况下,当访问者提交它时,它会将它们保持在同一页面上,并显示来自引导的javascript弹出窗口,说“谢谢,我们已经收到您的信息,将与您保持联系!” 但当我尝试显示Javascript时(目前仅使用基本警报),它看起来不像是执行的。。。相反,它只是在一个新窗口contact.php中编写代码 这是我正在使用的页面: 联络表格: <?php /* * Contact Form Class *

这是我正在使用的表单。这不是我做的东西,是在引导程序中找到的,然后把它移了过来。它可以工作,但理想情况下,当访问者提交它时,它会将它们保持在同一页面上,并显示来自引导的javascript弹出窗口,说“谢谢,我们已经收到您的信息,将与您保持联系!”

但当我尝试显示Javascript时(目前仅使用基本警报),它看起来不像是执行的。。。相反,它只是在一个新窗口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 = '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();

?>


感谢您的回复

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

因此您构建了类似于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
      });
 });