简体   繁体   中英

PHP and AJAX issue with sending data

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 = 'dvvsfb1@gmail.com';

$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?

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';
}

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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