表单通过 ajax POST 提交两次

form submitting twice via ajax POST

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

Issue: When calling php file from ajax. it executed twice and getting response as duplicate record.

好吧,我尝试 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 通过 AJAX 在按钮单击 addcountrybtn 时调用,并写入 div load-modal 中 html文件。

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

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

我所看到的几件事可能是原因。

如果您使用的是 IE,我已经看到在执行 POST 之前立即执行 GET(到相同的 URL,发送相同的数据),所以它可能是值得尝试在您的服务器上检查(并忽略 GET)

可能会在 AJAX 调用后将以下内容添加到按钮单击事件的末尾(实际上,通常我会把第一行放在顶部,并带有阻止默认值,而return 声明显然放在最后)...

e.stopImmediatePropagation();
return false;