简体   繁体   English

在PHP联系表中调用Javascript

[英]Call Javascript in PHP Contact Form

Here's the form I'm working with. 这是我正在使用的表格。 It's not something I made, found it in a bootstrap and moved it over. 这不是我做的,而是在引导程序中找到并移到了上面。 It works, but ideally, when the visitor submits it, it keeps them on the same page and displays a javascript popover from bootstrap, saying "Thanks, we've received your info and will be in touch!" 它可以正常工作,但理想情况下,当访客提交它时,它将它们保留在同一页面上,并显示来自引导程序的javascript弹出窗口,说:“谢谢,我们已经收到您的信息,并会与您联系!”

When I try and display Javascript though (just using a basic alert for now), it doesn't look like it executes... instead, it simply writes the code in a new window, contact.php. 当我尝试显示Javascript时(目前仅使用基本警报),它看起来不像是在执行……相反,它只是在新窗口contact.php中编写了代码。

Here's the page I'm working with: 这是我正在使用的页面:

http://www.jackalopemedia.com/benefits http://www.jackalopemedia.com/benefits

The contact form: 联系表格:

<?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();

?>

Thanks for any responses! 感谢您的任何回复!

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

So you've built something like an API receiver. 因此,您已经构建了类似于API接收器的工具。 What you need to do is use jQuery to post to contact.php asynchronously and and write the response to the page. 您需要做的是使用jQuery异步发布contact.php并将响应写入页面。

You can add some JS to your page like this: 您可以像这样向页面添加一些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
      });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM