douchuoxuan3177 2015-06-29 12:19
浏览 33
已采纳

PHP联系表单与Jquery验证无法正常工作

Problem is, whatever I tried did not work. Tried my own script, tried everything, then found some on google and tried them but no, don't know what's missing but, it doesn't work. The closest so far, I had empty emails. This is the latest version. Can anyone tell me what's missing ?

HTML

<div class="container-fluid">
                        <p id="returnmessage"></p>
                        <form action="sendEmail.php" id="contact_form" 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="contactName" id="contactName" 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="contactEmail" id="contactEmail" value="" class="required requiredField email" placeholder="Eposta"/>
                                    </div>
                                </li>
                                <li>
                                        <input type="text" style="border:1px solid rgba(220,220,220,0.5)" name="contactSubject" id="contactSubject" value="" class="hidden" placeholder=""/>
                                </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="contactMessage" style="border:1px solid rgba(220,220,220,0.5)" id="contactMessage" 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">
                                    <div id="cf_submit_p">
                                    <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" id="send_message"><i class="fa fa-paper-plane-o" style="font-size:20px;color:#3f97cf"></i></button>
                                    </div>
                                </li>
                            </ul>
                        </form>
                    </div> <!--end container-fluid-->
                </div><!-- #contact-canvas -->

JS validation

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

And PHP

<?php



//Değiştir. Site admini eposta adresi. 

$akaEmail = 'kreaft@outlook.com';





if($_POST) {



   $name = trim(stripslashes($_POST['contactName']));

   $email = trim(stripslashes($_POST['contactEmail']));

   $subject = trim(stripslashes($_POST['contactSubject']));

   $contact_message = trim(stripslashes($_POST['contactMessage']));



   // İsim doğrulama

    if (strlen($name) < 2) {

        $error['name'] = "Lütfen adınızı girin.";

    }

    // Email doğrulama

    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {

        $error['email'] = "Lütfen geçerli bir eposta adresi girin.";

    }

    // Mesaj doğrulama

    if (strlen($contact_message) < 15) {

        $error['message'] = "Lütfen mesajınızı girin. (Min. 15 karakter)";

    }

   // Konu

    if ($subject == '') { $subject = "Site İletişim Formu"; }





   // Mesajı oluşturuyoruz

   $message .= "Gönd.: " . $name . "<br />";

   $message .= "Eposta: " . $email . "<br />";

   $message .= "Mesaj: <br />";

   $message .= $contact_message;

   $message .= "<br /> ----- <br /> Bu eposta site iletişim formu üzerinden gönderilmiştir. <br />";



   // Başlıkları oluşturuyoruz

   $from =  $name . " <" . $email . ">";



   // Başlıklar

    $headers = "From: " . $from . "
";

    $headers .= "Reply-To: ". $email . "
";

    $headers .= "MIME-Version: 1.0
";

    $headers .= "Content-Type: text/html; charset=ISO-8859-1
";





   if (!$error) {



      ini_set("sendmail_from", $akaEmail); // windows server için

      $mail = mail($akaEmail, $subject, $message, $headers);



        if ($mail) { echo "OK"; }

      else { echo "Bir hata oluştu. Lütfen tekrar deneyin."; }



    } # hiç bir doğrulama hatası çıkmazsa bitir, çıkarsa hataları listeliyoruz.



    else {



        $response = (isset($error['name'])) ? $error['name'] . "<br /> 
" : null;

        $response .= (isset($error['email'])) ? $error['email'] . "<br /> 
" : null;

        $response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;



        echo $response;



    } # bir doğrulama hatası olmuşsa burada bitir.



}



?>
  • 写回答

2条回答 默认 最新

  • dongzhan8001 2015-06-29 12:24
    关注

    Notice: Undefined variable: message in /Users/kobigo/Sites/yedikitamuhendislik.com/sendEmail.php on line 61

    Look at what appears to be line 61:

    $message .= "Gönd.: " . $name . "<br />";
    

    You're trying to append to $message, but you never defined it in the first place. You can append to it after it's been defined, but on this first line you need to simply define it and assign to it:

    $message = "Gönd.: " . $name . "<br />";
    

    The remaining similar lines can append to it.


    You're also attaching to the wrong event in your JavaScript code:

    $('#send_message').on('submit',function(e){
    

    #send_message is a button, not a form. It doesn't have a submit event. Attach to the form instead:

    $('#contact_form').on('submit',function(e){
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?