通过ajax POST提交表单两次

5

通过使用php文件调用AJAX,将数据插入mysql。在执行insert语句之前,php代码执行select查询以查找重复记录并继续执行insert statement

问题: 从ajax调用php文件时,它会执行两次,并且得到重复记录的响应。

好的,我尝试使用error_log从插入函数中进行调用,结果它被调用了两次。

表单验证的触发点

$("#load-modal").on("click","#addcountryformsubmitbtn",function(e){
    e.preventDefault();
    var $form = $("#addcountryform"), $url = $form.attr('action');  
    $form.submit();
});

这是表单在验证后提交的方式:
}).on('success.form.bv', function(e){
    e.preventDefault();
    var $form = $("#addcountryform"), $url = $form.attr('action'); 
    $.post($url,$form.serialize()).done(function(dte){ $("#load-modal").html(dte); });
});

使用bootstrapvalidator、Core PHP、mysqli和Chrome浏览器进行开发。

实际的JS代码:

            $(document).ready(function() {
            $php_self_country="<?php echo $_SERVER['PHP_SELF']."?pg=countrycontent"; ?>";
            $("#country-content").load($php_self_country,loadfunctions);
            $("#country-content").on( "click", ".pagination a", function (e){
                e.preventDefault();
                $("#country-loading-div").show();
                var page = $(this).attr("data-page");
                $("#country-content").load($php_self_country,{"page":page}, function(){
                    $("#country-loading-div").hide();
                    loadfunctions();
                });             
            });
            $("#country-content").on("click","#closebtn",function(e){
                e.preventDefault();
                $("#country-content").load($php_self_country,loadfunctions);
            });
        });
        function loadfunctions(){
            $("[data-toggle='tooltip']").tooltip(); 
            $("#country-content").on("click","#addcountrybtn, #addcountrylargebtn",function(e){
                e.preventDefault();
                $.ajax({
                    url: $php_self_country,
                    type: "POST",
                    data: { 'addcountry':'Y' },
                    dataType: "html",
                    cache: false
                }).done(function(msg){
                    $("#load-modal").html(msg);
                    $("#load-modal").modal('show');
                    $('input[type="radio"]').iCheck({ checkboxClass: 'icheckbox_minimal', radioClass: 'iradio_minimal' });
                    modalvalidation();
                }).fail(function() {
                    $("#load-modal").html( "Request Failed. Please Try Again Later." );
                });         
            });
            $("#country-content").on("click",".tools a",function(e){
                e.preventDefault();
                var recordid = $(this).attr("record-id");
                $.ajax({
                    url: $php_self_country,
                    type: "POST",
                    data: { 'modifycountry':recordid },
                    dataType: "html",
                    cache: false
                }).done(function(msg){
                    $("#load-modal").html(msg);
                    $("#load-modal").modal('show');
                    $('input[type="radio"]').iCheck({ checkboxClass: 'icheckbox_minimal', radioClass: 'iradio_minimal' });
                    modalvalidation();
                }).fail(function() {
                    $("#load-modal").html( "Request Failed. Please Try Again Later." );
                });
            });
            $("#load-modal").on("click","#addcountryformsubmitbtn",function(e){
                e.preventDefault();
                var $form = $("#addcountryform"), $url = $form.attr('action');  
                $form.submit();
            });
            $("#load-modal").on("hide.bs.modal", function () {
                window.location.href=$php_self_country_pg;
            });
        }
        function modalvalidation(){ 
            $('#addcountryform').bootstrapValidator({
                message: 'This value is not valid',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    [-------Validation part comes here----------]
                }
            }).on('success.form.bv', function(e){
                e.preventDefault();
                var $form = $("#addcountryform"), $url = $form.attr('action'); 
                $.post($url,$form.serialize()).done(function(dte){ $("#load-modal").html(dte); });
            });
        }

HTML

这段 HTML 代码是通过按钮点击事件 addcountrybtn,利用 AJAX 技术并写入到基础 HTML 文件中的 load-modal div 中。

<div class="modal-dialog">
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title"><i class="fa fa-exchange"></i> <?php echo COUNTRYLABEL; ?></h4>
    </div>      
    <div class="modal-body">
        <form role="form" method="POST" action="self.php" name="addcountryform" id="addcountryform" class="form-horizontal">
            <div class="form-group">
                <div class="col-xs-3">
                <label for="countryname" class="pull-right">Country Name</label>
                </div>
                <div class="col-xs-9">
                <div class="lnbrd">
                <input type="text" class="form-control" name="countryname" placeholder="Enter Country Name">
                </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-3">
                <label for="crncyname" class="pull-right">Currency Name</label>
                </div>
                <div class="col-xs-9">
                <div class="lnbrd">
                <input type="text" class="form-control" name="crncyname" placeholder="Enter Currency Name">
                </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-3">
                <label for="crncycode" class="pull-right">Currency Code</label>
                </div>
                <div class="col-xs-9">
                <div class="lnbrd">
                <input type="text" class="form-control" name="crncycode" placeholder="Enter Currency Code">
                </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-3">
                <label for="forrate" class="pull-right">Foreign Currency Rate<?php echo isset($icon)?$icon:''; ?></label>
                </div>
                <div class="col-xs-9">
                <div class="lnbrd">
                <input type="text" class="form-control" name="forrate" placeholder="Enter Foreign Currency Rate.">
                </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-3">
                <label for="taxpercent" class="pull-right">Tax &#37;</label>
                </div>
                <div class="col-xs-9">
                <div class="lnbrd">
                <input type="text" class="form-control" name="taxpercent" placeholder="Enter Tax Percentage">
                </div>
                </div>
            </div>
        </form>         
    </div>
    <div class="modal-footer clearfix">
        <button type="button" class="btn btn-danger pull-right" id="addcountryformsubmitbtn">Add</button>
    </div>
</div>

注意:从数据库角度来看,代码的工作符合预期。


你能展示一下 #addcountryformsubmitbtn 的 HTML 代码吗?请检查它是否有 type="button" 属性。 - sp00m
@sp00m 在问题中找到 HTML。尝试了 type="button"type="submit",输出仍然相同。 - Syed Nizamudeen
1个回答

1

我看到有几个可能导致问题的原因。

如果你正在使用IE,我看到它会在执行POST之前立即进行GET(到相同的URL,发送相同的数据),因此可能值得在服务器上检查它(并忽略GET)。

还有一件事是,在AJAX调用之后,在你的按钮单击事件末尾添加以下内容(实际上,通常我会将第一行放在顶部,防止默认情况下,返回语句显然最后出现)...

e.stopImmediatePropagation();
return false;

它正常运行了!!!只是在所有的“e.preventDefault();”后面添加了“e.stopImmediatePropagation();”,并没有使用“return false;”。将来会有问题吗??在IE10和Chrome中进行了测试 :) - Syed Nizamudeen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接