三生石@ 2015-11-03 12:22 采纳率: 100%
浏览 30

用ajax提交表单

I'm building a website with GitHub pages and Jekyll.

I want to build a contact form where userss can send emails. For this I'm using Formspree . I created an account and I tried and it's working. The problem is that when an email is sent, i don't want to redirect the page at default thanks page. I tried to submit it with ajax but something is wrong and for that it's not working!! As result I have a 404 error. Also URL is changed and it looks like this: /?name=Name+Lastname&_replyto=test%40hotmail.com&message=Email+content+ Can somebody help me! Here's my code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.min.js"></script>
<script>
    $("#contact-form").validate({
  submitHandler: function(form) {
    $.ajax({
      url: "//formspree.io/egzontina.krasniqi@hotmail.com", 
      method: "POST",
      data: {
        name: $(form).find("input[name='name']").val(),
        _replyto: $(form).find("input[name='_replyto']").val(),
        message: $(form).find("textarea[name='message']").val()
      },
      dataType: "json",
      success: function() {
        $("#submit-success").fadeIn();
        $("#contact-form").fadeOut();
      },
      error: function() {
        $("#submit-errors").fadeIn();        
      }
    });
  }
});
</script>

Here's my html code:

 <div id="submit-success" class="alert alert-success alert-dismissible collapse" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          Message received! I'll be in touch.
 </div>


<div id="submit-errors" class="alert alert-danger alert-dismissible collapse" role="alert">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  It looks like there was an error submitting the form. Please try again later.
</div>


<form id="contact-form" class="form" action="/">
  <div class="form-group">
    <label for="name">Name</label>
    <input class="form-control" type="text" name="name" required placeholder="Name">
  </div>
  <div class="form-group">
    <label for="email">Email</label>
    <input class="form-control" type="email" name="_replyto" required placeholder="email@address.com">
  </div>
  <div class="form-group">
    <label for="message">Message</label>
    <textarea class="form-control" name="message" placeholder="Message" required rows="5"></textarea>
  </div>
  <input class="btn btn-primary" type="submit" value="Send">
</form>
  • 写回答

3条回答 默认 最新

  • weixin_33736048 2015-11-04 04:32
    关注

    Remove the action="/" from your form, leaving you with:

    <form id="contact-form" class="form">
    

    Try that and let me know what happens.

    Yh and change the submit input to a button

    <button type="button" class="btn btn-primary">Send</button>
    

    @Subhasish contribution is correct too. vote our answers pls. Let's know if u find it helpful

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用