【问题标题】:form submitting twice via ajax POST表单通过 ajax POST 提交两次
【发布时间】:2016-02-29 10:25:57
【问题描述】:

使用通过AJAX 调用的php 文件插入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 文件中的 div load-modal。

<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 找到有问题的 HTML。 都试过了 type="button" 和 type="submit" 输出还是一样的。

标签: javascript jquery ajax twitter-bootstrap bootstrapvalidator


【解决方案1】:

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

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

可能在 AJAX 调用之后将以下内容添加到按钮单击事件的末尾(实际上,通常我会将第一行放在最前面,并使用 prevent default,并且 return 语句显然是最后的)...

e.stopImmediatePropagation();
return false;

【讨论】:

  • 它的工作!刚刚在所有e.preventDefault(); 旁边添加了e.stopImmediatePropagation(); 并且没有使用return false;;以后会不会出问题??在 IE10 和 Chrome 中测试:)
猜你喜欢
  • 2011-06-17
  • 1970-01-01
  • 2015-04-04
  • 1970-01-01
  • 2017-02-18
  • 2015-05-21
  • 2013-12-10
  • 2013-05-20
  • 1970-01-01
相关资源
最近更新 更多