douwen0612
2019-03-26 23:12
浏览 64

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

I am building a portfolio website and in the contact form, I want a success/fail response after submission. I prefer to stay on the same page whilst this process.

I tried other solutions that were in other pages related to this topic but I could not understand why they did not work for me. Since I am a newbie, I will appreciate a crystal clear explanation. Thanks a lot!

I tried the solution under this topic: Submit contact form with success/fail alert on same page

MY CURRENT HTML CODE IS;

<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>

I expect to see confirmation or fail report on the same page after submitting the form. It can be anywhere on the page. I just want the visitor to know that I received their message or could not.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douchuang1861 2019-03-26 23:24
    已采纳

    Simple example jQuery version:

    $("#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;
    });
    

    Remember to return from server JSON like { success: true } or { error: true, msg: "message why fails" }.

    But it will be much better, when You will change input type=submit to button, and then make:

    $("#contact-form .class-of-input-button").off().on('click', function() { /* rest of the code */ });
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题