dongxianchu3541 2015-05-11 21:35
浏览 67

在php jquery中ajax调用成功后允许表单操作提交

<form id="formId" method="POST" action="#">

</form>

<script>
 $(document).ready(function () {
        $("#formId").submit(function (event) {

            alert("Handler for .submit() called.");
            var data= $("#txtDriverpassword").val();
            var data1= $("#driverId").val();

            $.ajax({
                type: "GET",
                url: "page.php",
                data: {"data": data, "data1": data1, "method": "verifypassword"},
                success: function (result) {

                    if (result === "valid") {
                        // alert('valid credentials....');

return true;

                    }
                    else {
                        alert("invalid credentials");
                       return false;

                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    //  alert("Status: " + textStatus); alert("Error: " + errorThrown); 
                    $("#successMessage").html(textStatus);
                }
            });
            //event.preventDefault();
        });
    });
<script>

Inside the form I have a table with dynamic data and validation for it using foreach. Now for one single input validation I just used an ajax call - if ajax call is a success, the form needs to submit. The query for insertion is written in the same page. can i know how to solve it? preventing default is working, but removing default event in the ajax success call is not working?

How to solve?

  • 写回答

2条回答 默认 最新

  • duan62819774 2015-05-11 21:45
    关注

    Because the ajax-call is asynchronous you have to prevent the first form submit and then when it's validated true make a new form submit. I haven't tried it but this should work (notice the lock variable I introduced):

    <script>
     $(document).ready(function () {
            var lock = false;
            $("#formId").submit(function (event) {
                if(!lock) {
                    alert("Handler for .submit() called.");
                    var driverPassword = $("#txtDriverpassword").val();
                    var driverId = $("#driverId").val();
    
                    $.ajax({
                        type: "GET",
                        url: "page.php",
                        data: {"data": data, "data1": dataaa, "method": "verifypassword"},
                        success: function (result) {
    
                            if (result === "valid") {
                                // alert('valid credentials....');
                                $("#formId").submit();
                            }
                            else {
                                alert("invalid credentials");
    
                            }
                            lock = true;
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            //  alert("Status: " + textStatus); alert("Error: " + errorThrown); 
                            $("#successMessage").html(textStatus);
                        }
                    });
                    event.preventDefault();
                }
            });
        });
    <script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图