简体   繁体   中英

PHP - Jquery contact form sends blank emails

I've been searching for a solution. Seen other topics, none was helpful so far. So wanted to ask mine. Sorry if it's duplicating another question.

Here is my html markup in <head></head>

    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
     ....
    <style>
    .error { display:none; }
    .success { display:none; }
    </style>


<script type="text/javascript">
    $(document).ready(function(){
        $('#send_message').submit(function(e){
            e.preventDefault();
            var error = false;
            var name = $('#name').val();
            var email = $('#email').val();
            var message = $('#message').val();
            if(name.length == 0){
                var error = true;
                $('#name_error').fadeIn(500);
            } else {
                $('#name_error').fadeOut(500);
            }
            if(email.length == 0 || email.indexOf('@') == '-1'){
                var error = true;
                $('#email_error').fadeIn(500);
            } else {
                $('#email_error').fadeOut(500);
            }
            if(message.length == 0){
                var error = true;
                $('#message_error').fadeIn(500);
            } else {
                $('#message_error').fadeOut(500);
            } if(error == false){
                $('#send_message').attr({'disabled' : 'true', 'value' : 'Gönderiliyor...' });
                $.post("send_message.php", $("#contact_form").serialize(),function(result){
                    if(result == 'sent'){
                        $('#cf_submit_p').remove();
                        $('#mail_success').fadeIn(500);
                    } else {
                        $('#mail_fail').fadeIn(500);
                        $('#send_message').removeAttr('disabled').attr('value', 'Gönder');
                    }
                });
            }
        });
    });
</script>

in <body></body>

<div class="container-fluid">
                        <p id="returnmessage"></p>
                        <form action="send_message.php" id="contactForm" method="post">
                            <h2 style="font-size:14px;line-height:18px;font-weight:600;padding-bottom:0;">Bize Yazın</h2>
                            <ul class="contactform">
                                <li>
                                    <div id="name_error" class="error" style="color:#aa3939; font-size:8px; line-height:8px;"> <i class="fa fa-exclamation"></i> Lütfen adınızı giriniz.</div>
                                    <span class="contact-input-icon" style="text-align:left"><i class="fa fa-user"></i></span>
                                    <div class="input-field">
                                        <input type="text" style="border:1px solid rgba(220,220,220,0.5)" name="name" id="name" value="" class="required requiredField" placeholder="Ad Soyad"/>
                                    </div>
                                </li>
                                <li>
                                    <div id="email_error" class="error" style="color:#aa3939; font-size:8px; line-height:8px;"> <i class="fa fa-exclamation"></i> Lütfen eposta adresinizi giriniz.</div>
                                    <span class="contact-input-icon"><i class="fa fa-envelope"></i></span>
                                    <div class="input-field">
                                        <input type="email" style="border:1px solid rgba(220,220,220,0.5)" name="email" id="email" value="" class="required requiredField email" placeholder="Eposta"/>
                                    </div>
                                </li>
                                <li class="textarea">
                                    <div id="message_error" class="error" style="color:#aa3939; font-size:8px; line-height:8px;"> <i class="fa fa-exclamation"></i> Lütfen mesajınızı giriniz.</div>
                                    <span class="contact-input-icon"><i class="fa fa-pencil"></i></span>
                                    <div class="input-field">
                                        <textarea name="message" style="border:1px solid rgba(220,220,220,0.5)" id="message" rows="6" cols="20" class="required requiredField" placeholder="Mesajınız"></textarea>
                                    </div>
                                    <div id="mail_success" class="success" style="color:#00CC00"><i class="fa fa-check"></i> İlginiz için teşekkürler. En kısa sürede sizinle irtibata geçeceğiz.</div>
                                    <div id="mail_fail" class="error" style="color:#aa3939"><i class="fa fa-times"></i> Üzgünüz, mesajınız iletilemedi. Daha sonra lütfen tekrar deneyin.</div>
                                </li>
                                <li class="buttons">
                                    <input type="hidden" style="border:1px solid rgba(220,220,220,0.5)" name="submitted" id="submitted" value="true" />
                                    <button type="submit" style="border:1px solid #3f97cf" class="button cf_submit_p" id="send_message"><i class="fa fa-paper-plane-o" style="font-size:20px;color:#3f97cf"></i></button>
                                </li>
                            </ul>
                        </form>
                    </div> <!--end container-fluid-->

and in send_message.php file;

    <?php

    if($_SERVER['REQUEST_METHOD'] === 'POST'){

    //önce değişkenleri topluyoruz.
    $email_to =   'yeterkara90@gmail.com'; //epostanın gönderileceği adres
    $name     =   $_POST['name'];
    $email    =   $_POST['email'];
    $message  =   $_POST['message'];

    $msgTo = "From: $name \r\nEmail: $email \r\nMessage: \r\n$message";

    /*$header değişkeni ilave olanaklar için. Böylelikle yanıtla ya da ilet dediğimizde,
     posta kutusu kime cevap vereceğini bilecek */
    $headers  = "FROM: $email\r\n";
    $headers .= "REPLY-TO: $email\r\n";


    if(mail($email_to, $msgTo, $headers)){
        echo 'sent'; // Mailin gönderildiğini AJAXa paslıyoruz.
    }else{
        echo 'failed';// ... ya da gönderilemediğini
    }

}
?>

There's something missing, checked the class and id names many times, tried altering the names many times, but I guess, the form does not get the values.. The validation part works great, even displays success message after submit, and sends emails too.. But only the From, Message, Email parts, not the variables.

Can you please help.

Regards,

Vis.

您的表单ID为contactForm,并且您正在使用#contact_form

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