简体   繁体   English

表单提交js / jquery停留在同一页面上

[英]Form submission js/jquery staying on same page

I'm trying to submit a form inside a CMS page and don't want a new page to load after processing the php mail script. 我正在尝试在CMS页面内提交表单,并且不希望在处理php邮件脚本后加载新页面。 Instead I need to display a single line success message (not an alert pop up box) without reloading the page or going to a different page. 相反,我需要显示单行成功消息(而不是警报弹出框),而无需重新加载页面或转到其他页面。

Here's the code I have and my understanding is that the event.preventDefault should allow to stay on same page and $("#contactResponse").html(data); 这是我的代码,我的理解是event.preventDefault应该允许保持在同一页面和$("#contactResponse").html(data); should put the success message on the same page. 应该将成功消息放在同一页面上。

This is my div tag above the form which is supposed to receive the success message (I've tried putting it after my form too): 这是我的div标签,它应该接收成功消息(我已经尝试将它放在我的表单之后):

<div id="contactResponse"></div>

This is my form tag: 这是我的表格标签:

Edit: Including my form code as well: (The div class stuff is from a custom css that someone else has done) 编辑:包括我的表单代码:( div类的东西来自其他人已经完成的自定义css)

<form id="contactForm" name="contactForm" method="post" action="/myemail.php">

<div class="form-group"><input class="form-control" name="email" type="email" placeholder="Email Address" /></div>
</div>
<div class="col-sm-4">
<div class="form-group"><input class="form-control" name="question" type="text" placeholder="What is your question?" /></div>
</div>
<div class="form-group"><input class="btn btn-warning btn-block" type="submit" value="Request Information"></div>
</form>

This is the script above my form and div tag: 这是我的表单和div标签上方的脚本:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
    $("#contactForm").submit(function(event) {
        event.preventDefault();

        var $form = $(this),
             $submit = $form.find('button[type="submit"]'),
             email_value = $form.find('input[name="email"]').val(),
             message_value = $form.find('input[name="question"]').val(),
             url = $form.attr('action');

        var posting = $.post(url, { 
            email: email_value, 
            question: message_value 
        });

        posting.done(function(data) {
            $("#contactResponse").html(data);
        });
    });
</script>

The email works but the php script is on the server, and it takes me to a different page. 电子邮件有效但php脚本在服务器上,它将我带到另一个页面。

Can someone please give me some suggestions/advice. 有人可以给我一些建议/意见。

Thanks 谢谢

Try this, it works for me 试试这个,它对我有用

<script>
  // Get the form.
  var form = $('#contactForm');
  // Get the messages div.
  var formMessages = $('#contactResponse');
  // Set up an event listener for the contact form.
  $(form).submit(function(event) {
    // Stop the browser from submitting the form.
    event.preventDefault();
    // Serialize the form data.
    var formData = $(form).serialize();
    // Submit the form using AJAX.
    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: formData
    }).done(function(response) {
        // Make sure that the formMessages div has the 'success' class.
        $(formMessages).removeClass('error');
        $(formMessages).addClass('success');
        // Set the message text.
        $(formMessages).text(response);
        // Clear the form.
        $('#name').val('');
        $('#email').val('');
        // $('#subject').val('');
        // $('#company').val('');
        $('#message').val('');
        $(form).hide();
    }).fail(function(data) {
        // Make sure that the formMessages div has the 'error' class.
        $(formMessages).removeClass('success');
        $(formMessages).addClass('error');
        $( "#contact" ).children(".loading").remove();
        // Set the message text.
        if (data.responseText !== '') {
            $(formMessages).text(data.responseText);
        } else {
            $(formMessages).text('Oops! An error occured and your message could not be sent.');
        }
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM