duanba8070 2016-04-05 15:05
浏览 39
已采纳

自定义ajax发送邮件表单

I got this HTML form code :

        <form id="main-contact-form" name="contact-form">
            <div class="row  wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
              <div class="col-sm-6">
                <div class="form-group">
                  <input type="text" id="name" name="name" class="form-control" placeholder="Name" required>
                </div>
              </div>
              <div class="col-sm-6">
                <div class="form-group">
                  <input type="email" id="email" name="email" class="form-control" placeholder="Email Address" required>
                </div>
              </div>
            </div>
            <div class="form-group">
              <input type="text" id="subject" name="subject" class="form-control" placeholder="Subject" required>
            </div>
            <div class="form-group">
              <textarea name="message" id="message" class="form-control" rows="4" placeholder="Enter your message" required></textarea>
            </div>                        
            <div class="form-group">
              <button type="submit" class="btn-submit">Send Now</button>
            </div>
          </form>

and the corresponding jQuery code which I think it sends the informations :

// Contact form
var form = $('#main-contact-form');     
form.submit(function(event){
    event.preventDefault();

    var form_status = $('<div class="form_status"></div>');
    $.ajax({
        url: $(this).attr('action'),
        beforeSend: function(){
            form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
        }
    }).done(function(data){
        form_status.html('<p class="text-success">Thank you for contact us. As early as possible  we will contact you</p>').delay(3000).fadeOut();
    });
});

When I submit the form I got nothing in my webmail. Can anyone spot the problem ?

Thanks

  • 写回答

1条回答 默认 最新

  • dpnvrt3119 2016-04-05 15:08
    关注

    Well you should have told me you want to use ajax. So you should do this :

    In jour jQuery :

    // Contact form
    var form = $('#main-contact-form');     
    form.submit(function(event){
        event.preventDefault();
    
        var name = $('#name').val();
        var subject = $('#subject').val();
        var email = $('#email').val();
        var message = $('#message').val();          
    
        var form_status = $('<div class="form_status"></div>');
        $.ajax({
            url: $(this).attr('action'),
            type: 'POST',
            data: {name: name, subject:subject, email:email, message:message }, 
            beforeSend: function(){
                form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
            }
        }).done(function(data){
            form_status.html('<p class="text-success">Thank you for contact us. As early as possible  we will contact you</p>').delay(3000).fadeOut();
        });
    });
    

    and in your HTML leave it as it is.

    Tell me if it helped.

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

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下