douwen0612 2019-03-26 23:12
浏览 65
已采纳

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

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 */ });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码