dsfdsfdsfdsf1223 2016-05-24 10:27
浏览 82
已采纳

联系表单发送时无效的电子邮件

I get an "invalid email" upon sending, even though the email is correct. I am having doubts about this code. Could someone take a glance?

Here's the HTML

<form id="contact-form" action="email.php" method="post" class="clearfix">
    <div class="contact-box-hide">
        <div class="col-sm-6">
            <input type="text"  class="form-control" id="first_name" name="first_name" required placeholder="Navn">
            <span class="first-name-error"></span>
        </div>
        <div class="col-sm-6">
            <input type="text"  class="form-control" id="last_name" name="last_name" required placeholder="Efternavn">
            <span class="last-name-error"></span>
        </div>
        <div class="col-sm-6">
            <input type="email" class="form-control"  id="email" name="contact_email" required placeholder="Email">
            <span class="contact-email-error"></span>
        </div>
        <div class="col-sm-6">
            <input type="text"  class="form-control" id="subject" name="contact_subject" required placeholder="Hvor skal oplægget afholdes?">
            <span class="contact-subject-error"></span>
        </div>

    <!--<div class="col-sm-6">
            <label class=".radio-inline">
                <input type="radio" class="form-control" id="choice" name="options" required placeholder="Emne">
                <span class="contact-subject-error"></span>Option 1
            </label>
            <label class=".radio-inline" style="border-left: 0px;" >
                <input type="radio"  class="form-control" id="choice" name="options" required placeholder="Emne">
                <span class="contact-subject-error"></span>Option 2
            </label>                    
        </div>
        -->
        <!--<div class="col-sm-20">
            <input type="radio" class="form-control" id="contact_type" name="nummer 1" checked>
            <span class="contact-x-error"></span>
        </div>
        <div class="col-sm-20">
            <input type="radio" class="form-control" id="contact_type" name="nummer 1" checked>
            <span class="contact-x-error"></span>
        </div>
        -->
        <div class="col-sm-10">
            <textarea class="form-control" rows="5" id="message" name="message" required placeholder="Besked"></textarea>
            <span class="contact-message-error"></span>
        </div>
        <div class="col-sm-2">
            <button id="contact-submit" class="btn custom-btn col-xs-12" type="submit" name="submit"><a href=""></a><i class="fa fa-angle-right"></i></button>
            <span id="contact-loading" class="btn custom-btn col-xs-12"> <i class="fa fa-refresh fa-spin"></i> </span>
        </div>
    </div><!-- /.contact-box-hide -->
    <div class="contact-message"></div>
</form>     

Here's the email.php (the contact.php, which the .js is calling, is identical):

<?php
    if($_REQUEST['first_name'] == '' || $_REQUEST['contact_email'] == '' ||  $_REQUEST['message'] == ''):
        return "error";
    endif;
    if (filter_var($_REQUEST['contact_email'], FILTER_VALIDATE_EMAIL)):
        $subject = 'Fra Kriger til Kaemper - kontaktformular'; // Subject of your email
        // Receiver email address
        $to = 'geerrift@gmail.com';  //Change the email address by yours

        // prepare header
        $header = 'From: '. $_REQUEST['first_name'] . " " .$_REQUEST['last_name'] . ' <'. $_REQUEST['contact_email'] .'>'. "
";
        $header .= 'Reply-To:  '. $_REQUEST['first_name'] . " " .$_REQUEST['last_name'] . ' <'. $_REQUEST['contact_email'] .'>'. "
";
        // $header .= 'Cc:  ' . 'example@domain.com' . "
";
        // $header .= 'Bcc:  ' . 'example@domain.com' . "
";
        $header .= 'X-Mailer: PHP/' . phpversion();
        $message .= 'Name: ' . $_REQUEST['first_name'] . " " .$_REQUEST['last_name'] . "
";
        $message .= 'Email: ' . $_REQUEST['contact_email'] . "
";
        $message .= 'Subject: ' . $_REQUEST['contact_subject'] . "
";
        $message .= 'Message: '. $_REQUEST['message'];
        // Send contact information
        $mail = mail( $to, $subject , $message, $header );
        echo 'sent';
    else:
        return "error";
    endif; 
?>

Here is the .js:

$('#contact-submit').click(function () {
    $('.first-name-error, .last-name-error, .contact-email-error, .contact-subject-error, .contact-message-error').hide();
    var first_nameVal = $('input[name=first_name]').val();
    var last_nameVal = $('input[name=last_name]').val();
    var emailReg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/igm;
    var emailVal = $('#contact_email').val();
    var contact_subjectVal = $('input[name=contact_subject]').val();
    var messageVal = $('textarea[name=message]').val();

    //validate 

    if (first_nameVal == '' || first_nameVal == 'First Name *') {
        $('.first-name-error').html('<i class="fa fa-exclamation"></i> Vi har brug for dit fornavn.').fadeIn();
        return false;
    }
    if (last_nameVal == '' || last_nameVal == 'Last Name *') {
        $('.last-name-error').html('<i class="fa fa-exclamation"></i> Vi har brug for dit efternavn.').fadeIn();
        return false;
    }
    if (emailVal == "" || emailVal == "Email Address *") {

        $('.contact-email-error').html('<i class="fa fa-exclamation"></i> Vi har brug for din mailadresse.').fadeIn();
        return false;

    } else if (!emailReg.test(emailVal)) {

        $('.contact-email-error').html('<i class="fa fa-exclamation"></i> Ugyldig mailadresse.').fadeIn();
        return false;
    }
    if (contact_subjectVal == '' || contact_subjectVal == 'Subject *') {
        $('.contact-subject-error').html('<i class="fa fa-exclamation"></i> Subject is required.').fadeIn();
        return false;
    }
    if (messageVal == '' || messageVal == 'Message *') {
        $('.contact-message-error').html('<i class="fa fa-exclamation"></i> Skriv venligst en henvendelse.').fadeIn();
        return false;
    }

    var data_string = $('.contact-form').serialize();

    $('#contact-submit').hide();
    $('#contact-loading').fadeIn();
    $('.contact-error-field').fadeOut();

    $.ajax({
        type: "POST",
        url: "php/contact.php",
        data: data_string,

        //success
        success: function (data) {

            $('.contact-box-hide').slideUp();
            $('.contact-message').html('<i class="fa fa-check contact-success"></i><div>Your message has been sent.</div>').fadeIn();
        },
        error: function (data) {

            $('.btn-contact-container').hide();
            $('.contact-message').html('<i class="fa fa-exclamation contact-error"></i><div>Something went wrong, please try again later.</div>').fadeIn();
        }

    }) //end ajax call
    return false;
});

Please tell me if I've left anything out, and I'll provide it immediately!

  • 写回答

1条回答 默认 最新

  • dprxj1995 2016-05-24 10:35
    关注

    Change

    var emailVal = $('#contact_email').val();
    

    to

    var emailVal = $('#email').val();
    

    or

    var emailVal = $('input[name="contact_email"]').val();
    

    On your email field you have an ID of email and a NAME of contact_email. When using the hash (#) in jQuery you're searching for the ID, not the name. So your script wan't able to find the email value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?