简体   繁体   中英

Contact form sends blank emails

My contact form is not including message and other informations in emails.

There is 'Unknown sender' and 'No subject'. Email contains my tags (from:, subject:, message:) but no content is written after them.

I have tried a few other scripts, but they gave the same result.

My JS code

(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  = $('#rp_name').val(),
            email  = $('#rp_email').val(),
            subject  = $('#rp_subject').val(),
            message  = $('#rp_message').val(),
            data_html,
            success = $('#success');

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

        if(subject === ""){
            $('#rp_subject').val('Please enter your name.');
        }

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

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

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

            alert(data_html);
            $.ajax({
                type: 'POST',
                url: 'php_helpers/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>')  ;
                        $('#rp_name').val('');
                        $('#rp_email').val('');
                        $('#rp_message').val('');
                    }else{
                        success.html('<div class="alert alert-error">Message <strong>not</strong> sent! Please Try Again!</div>')  ; 
                    }
                }
            });

        }
        return false;
    });
});
})(jQuery);

My PHP code

<?php

$to = 'email'; // I changed it 

$subject = $_POST['subject'];

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

$fields = array(
    0 => array(
        'text' => 'Imie',
        'val' => $_POST['name']
    ),
    1 => array(
        'text' => 'Email',
        'val' => $_POST['email']
    ),
    2 => array(
        'text' => 'WIADOMOSC',
        'val' => $_POST['message']
    )
);

$message = "";

foreach($fields as $field) {
    $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}

$$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: '. $email . "\r\n" . 'X-Mailer: PHP/' . phpversion();


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

if ($message){
echo 'sent';
}else{
echo 'failed';
}
} else {
echo "Don't access this file directly"; 
}
?>

I'll be glad for any hints.

There as i check your contact.php file code is ok. But you need one thing if use firefox. please console your code.I mean when you open firebug there will be one option console. with help you can check is your value along with variable is going or they empty.

I think your value is not going to PHP file. use console and you can easily bug your value.

I hope this make sense for you.

Check if your php script receives name, email and message. You can check it using

var_dump($_POST);

If server doesn't receive these variables, try to send data_html as an object: change

data: data_html,

to

data: {name: name, email: email, message: message}

I didn't test it, but I always use this way of sending data from js to server.

You are using two dollar symbols in the line: $$headers = 'MIME-Version: 1.0' . "\\r\\n";
Please remove "$" and try again.

Thanks for all your responses. I finally manage to find a solution. I haven't used firebug before, but thanks to it I could find the error message. Now the script is working fine.

It wasn't in code, but in .htaccess rewrite rule for .php files which was couseing 301 error for ajax request (../contact instead of ../contact.php).

I have one .php page (file uploader) and I thought that adding another seo-friendly link rule for it would be a good idea.

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