简体   繁体   English

php中的灯箱联系表单

[英]lightbox contact form in php

I am now to this altogether. 我现在完全是这样。 I need the success message when the lightbox contact form is posted to appear in the lightbox. 张贴灯箱联系表单时,我需要成功消息以显示在灯箱中。 The mail is not sent out neither is the success message displayed. 邮件不会发送出去,也不会显示成功消息。

I have the following code in my site to collect the data and post it to a separate page that send out mail. 我的网站上有以下代码来收集数据并将其发布到发送邮件的单独页面上。 The thing is email is not sent out using the mail.php file. 问题是电子邮件没有使用mail.php文件发送出去。 Need help. 需要帮忙。

<script>
        function sendEmail() {
            // 1. Create XHR instance - Start

            var xhr;
            if (window.XMLHttpRequest) {
                xhr = new XMLHttpRequest();
            }

            else if (window.ActiveXObject) {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            }
            else {
                throw new Error("Form is not supported by this browser");
            }

            // 1. Create XHR instance - End

            // 2. Define what to do when XHR feed you the response from the server - Start

            xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                    if (xhr.status == 200 && xhr.status < 300) {
                        // document.getElementById('loadix').innerHTML = xhr.responseText;
                        if(xhr.responseText == "success"){
                            $('#confirmation').html("<h2><i>Thank you for Registering for the webinar. An email with a link to the webinar will be emailed to you soon.</i></h2>");
                            $('#tosend').val(0);
                        }
                        else {
                            $('#confirmation').html("<h2>Email sending failed</h2>");
                        }
                    }
                }
            }
            // 2. Define what to do when XHR feed you the response from the server - Start
            var message = document.getElementById("message").value;
    var subject = document.getElementById("subject").value;
            var email = document.getElementById("email").value;
    var Home_phone = document.getElementById("Home_phone").value;
    var full_name = document.getElementById("full_name").value;
    var day = document.getElementById("day").value;
            var tosend = document.getElementById("tosend").value;

            var post_str = "&message=" + message +"&subject=" + subject +"&email=" + email +"&Home_phone=" + Home_phone +"&full_name=" + full_name +"&day=" + day;

            // 3. Specify your action, location and Send to the server - Start 
            if(tosend == 1){
                xhr.open('POST', 'mail.php');
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xhr.send(post_str);
            }
            // 3. Specify your action, location and Send to the server - End

    return true;
      }

The mail.php has the following code. mail.php具有以下代码。

<?php

if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "xxx@gmail.com"; 
$full_name = $_POST['full_name']; // required 
$day = $_POST['day']; // required 
$email_from = $_POST['email']; // required
$Home_phone = $_POST['Home_phone']; // not required 
$subject = $_POST['subject']; // required
$message = $_POST['message']; // required

$email_message = "Webinar request details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string); 
}
$email_message .= "Message: ".clean_string($message)."\n";
$email_message .= "Name: ".clean_string($full_name)."\n";
$email_message .= "Home_phone: ".clean_string($Home_phone)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n"; 
$email_message .= "Day: ".clean_string($day)."\n";

// create email headers

$headers = 'From: '.$email_from."\r\n".
 'Reply-To: '.$email_from."\r\n" .
 'X-Mailer: PHP/' . phpversion();
 @mail($email_to, $email_subject, $email_message, $headers); 
}
?>

Maybe you will have a look at jQuery for an ajax call. 也许您将看看jQuery的ajax调用。 it is a short way to make a complex post request in less then 3 lines of code. 这是用不到3行代码进行复杂的发布请求的一种简短方法。 with the jqXHR Objects you can handle the response. 使用jqXHR对象,您可以处理响应。 http://api.jquery.com/jQuery.post/ http://api.jquery.com/jQuery.post/

in mail.php you have used the mail function. 在mail.php中,您已经使用了邮件功能。 at the beginning you used the @, that means that the mail function will not throw any error. 在开始时,您使用@,这意味着mail函数将不会引发任何错误。 maybe you have to remove it to see any errors while sending the email. 也许您必须删除它才能在发送电子邮件时看到任何错误。

here an updated mail.php 这里是更新的mail.php

<?php
if (isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "xxx@gmail.com";
    $full_name = $_POST['full_name']; // required 
    $day = $_POST['day']; // required 
    $email_from = $_POST['email']; // required
    $Home_phone = $_POST['Home_phone']; // not required 
    $subject = $_POST['subject']; // required
    $message = $_POST['message']; // required

    $email_message = "Webinar request details below.\n\n";

    function clean_string($string) {
        $bad = array("content-type", "bcc:", "to:", "cc:", "href");
        return str_replace($bad, "", $string);
    }

    $email_message .= "Message: " . clean_string($message) . "\n";
    $email_message .= "Name: " . clean_string($full_name) . "\n";
    $email_message .= "Home_phone: " . clean_string($Home_phone) . "\n";
    $email_message .= "Email: " . clean_string($email_from) . "\n";
    $email_message .= "Day: " . clean_string($day) . "\n";

    $headers = 'From: ' . $email_from . "\r\n" .
        'Reply-To: ' . $email_from . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    $error = @mail($email_to, $email_subject, $email_message, $headers);
    if ($error) {
        header($_SERVER['SERVER_PROTOCOL'] . ' Internal Server Error', true, 500);
    }
} else {
    header($_SERVER['SERVER_PROTOCOL'] . ' Bad Request', true, 400);
}

please remember that there is no validation for a correct email. 请记住,没有验证正确的电子邮件。 you form can be used for sending spam mails 您的表格可用于发送垃圾邮件

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

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