在同一页面上提交具有发送状态的联系表单

时间:2019-03-26 23:12:23

标签: javascript php html

我正在建立一个投资组合网站,并且在联系表格中,我希望提交后能成功/失败。在此过程中,我希望停留在同一页面上。

我尝试了与该主题相关的其他页面中的其他解决方案,但我不明白为什么它们不适合我。由于我是新手,因此我将不胜感激。非常感谢!

我在以下主题下尝试了解决方案:Submit contact form with success/fail alert on same page

我当前的HTML代码是;

<form id="contact-form" method="post" action="contact.php" role="form" enctype="text/plain">
                        <div class="messages"></div>
                        <div class="controls">
                            <div class="form-group">
                                <input id="form_name" type="text" name="name" class="form-control" placeholder="Name *" required="required" data-error="name is required.">
                                <div class="help-block with-errors"></div>
                            </div>
                            <div class="form-group">
                                <input id="form_email" type="email" name="email" class="form-control" placeholder="E-mail *" required="required" data-error="Valid email is required.">
                                <div class="help-block with-errors"></div>
                            </div>
                            <div class="form-group">
                                <input id="form_subject" type="text" name="subject" class="form-control" placeholder="Subject">
                                <div class="help-block with-errors"></div>
                            </div>
                            <div class="form-group">
                                <textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="4" required="required" data-error="send a message."></textarea>
                                <div class="help-block with-errors"></div>
                            </div>
                            <input type="submit" name="submit" class="btn btn-send float-" value="Send message">
                            <p class="text-muted"><strong>*</strong> These fields are required.</p>
                        </div>
                        <div class="clearfix"></div>
                    </form>

提交表格后,我希望在同一页面上看到确认或失败报告。它可以在页面上的任何位置。我只想让访问者知道我是否收到了他们的消息。

1 个答案:

答案 0 :(得分:2)

简单的示例jQuery版本:

$("#contact-form").submit(function() {
    var name = $("#form_name").val();
    var email = $("#form_email").val();
    // ...other fields
    // and validation if empty or e-mail is valid etc.
    // if validation fails just return false;

    if( !name ) {
        // here You can send message or activate help-block
        return false;
    }

    // if everything correct use for example ajax from jquery
    $.ajax({
        url: '/url/to/receiver',
        type: 'post',
        data: { name: name, .... },
        dataType: 'jsonp',
        success: function( r ) {
            if( r.success ) {
                // message and pass form validation 
                // clear fields, or hide contact form
            }

            if( r.error ) {
                // show error
            }
        }
    });

    return false;
});

请记住要从服务器JSON返回,例如{success:true}或{error:true,msg:“ message why failed”}。

但是,当您将input type = submit更改为button,然后进行以下操作时,效果会更好:

$("#contact-form .class-of-input-button").off().on('click', function() { /* rest of the code */ });