0

Just another silly question from the beginner. I have this function:

    $(document).ready(function() {
    $('#submit-form').click(function(e){

        e.preventDefault();
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var name     = $('#name').val(),
            email    = $('#email').val(),
            phone    = $('#phone').val(),
            date     = $('#date').val(),
            message  = $('#message').val(),
            data_html,
            success = $('#success');

        if(name == "")
            $('#name').val('Please enter your name.');

        if(phone == "")
            $('#phone').val('Please enter your phone number.');

        if(date == "")
            $('#date').val('Please enter a date and time.');

        if(email == ""){
            $('#email').val('Your email is required.');
        }else if(reg.test(email) == false){
            $('#email').val('Invalid Email Address.');
        }

        if(message == "")
            $('#message').val('Message is required.');

        if(message != "" && name != "" && reg.test(email) != false) {
            data_html = "name=" + name + "&email="+ email + "&message=" + message + "&phone="+ phone + "&date="+ date;

            //alert(data_html);
            $.ajax({
                type: 'POST',
                url: '../contact_form.php',
                data: data_html,
                success: function(msg){

                    if (msg == 'sent'){
                        success.html('<div class="alert alert-success">Message <strong>successfully</strong> sent!</div>')  ;
                        $('#name').val('');
                        $('#phone').val('');
                        $('#email').val('');
                        $('#date').val('');
                        $('#message').val('');

                    }else{
                      success.html('<div class="alert alert-error">Message <strong>NOT</strong> sent!  Please try again later. </div>')  ; 

                    }
                }
            });

        }
        return false;
   });
});

And I have created this PHP which may be wrong so please do not judge me... I am still learning :) I am a total beginner to this so please do not give me a hard time :)

 <?php


$to = '[email protected]';

$subject = 'Request a Booking';


if($to) {
$name = $_POST['name'];
$email = $_POST['email'];

$fields = array(
    0 => array(
        'text' => 'Name',
        'val' => $_POST['name']
    ),
    1 => array(
        'text' => 'Email address',
        'val' => $_POST['email']
    ),
    2 => array(
        'text' => 'Phone',
        'val' => $_POST['phone']
    ),
    3 => array(
        'text' => 'Date & Time',
        'val' => $_POST['date']
    ),
    4 => array(
        'text' => 'Message',
        'val' => $_POST['message']
    )
);

$message = "";

foreach($fields as $field) {
    $message .= $field['text'].": " . htmlspecialchars($field['val'],       ENT_QUOTES) . "<br>\n";
}
ini_set("SMTP","aspmx.l.google.com");
$headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=utf-8\r\n";
$headers .= "From: \"" . $name . "\" \r\n";
$headers .= "Reply-To: " .  $email . "\r\n";
$message = utf8_decode($message);

mail($to, $subject, $message, $headers);

?>

I am keep getting error message that message is not sent. It doesn't connect with my PHP i think. Any advice?

6
  • do you have mailserver installed ? Commented Feb 18, 2014 at 12:06
  • No im just trying to send it to my own email address. Commented Feb 18, 2014 at 12:11
  • press f12 see console window it shows any error Commented Feb 18, 2014 at 12:13
  • Your php scripts need to respond with 'sent'. Dont see that happening. Plus there could be several other issues like incorrect URL, SMTP (needs authentication) Commented Feb 18, 2014 at 12:15
  • u trying localhost or web server Commented Feb 18, 2014 at 12:16

2 Answers 2

1

You are not "answering" anything from your PHP file. Try replacing your "mail()" call with this:

if (mail($to, $subject, $message, $headers)) {
    echo 'sent';
} else {
    echo 'not sent';
}
Sign up to request clarification or add additional context in comments.

Comments

0

In the ajax success callback, you are checking for

if (msg == 'sent')

where msg is the response response of the ajax request.

But in your php file, you are not sending any response. Try,

$status = mail($to, $subject, $message, $headers);
echo $status ? 'sent' : 'failed';

And in the Ajax success callback,

if($.trim(msg) == 'sent') {
    //some code
}

3 Comments

Whats the response you are getting? Check using firebug or any dev tools?`
On a site im getting message not sent - which is correct. But why it wouldn't be sent? in console i am getting: console.markTimeline is deprecated. Please use the console.timeStamp instead.
Check if your mail server settings are proper.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.