0

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. 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.

<?php

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

$email_to = "[email protected]"; 
$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); 
}
?>

1 Answer 1

0

Maybe you will have a look at jQuery for an ajax call. it is a short way to make a complex post request in less then 3 lines of code. with the jqXHR Objects you can handle the response. http://api.jquery.com/jQuery.post/

in mail.php you have used the mail function. at the beginning you used the @, that means that the mail function will not throw any error. maybe you have to remove it to see any errors while sending the email.

here an updated mail.php

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

    $email_to = "[email protected]";
    $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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.