表单通过ajax POST提交两次 [英] form submitting twice via ajax POST

查看:101
本文介绍了表单通过ajax POST提交两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 php 插入 mysql 通过 AJAX 。在插入语句之前,php代码执行选择查询查找重复记录并继续 insert语句

Inserting into mysql using php file called via AJAX. Before insert statement php code performs select query to find duplicate records and continue to insert statement.


问题:从中调用php文件时阿贾克斯。它执行了两次,并将响应作为重复记录。

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

我试过 error_log 从插入函数调用两次。

well i tried error_log from insert function its called twice.

表单验证的触发点

$("#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 调用并写入div load-modal 位于基本html文件中。

this html is called on button click addcountrybtn via AJAX and write in to div load-modal which is in base html file.

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

注意: - 在数据库点视图代码按预期工作。

Note:- in Database point of view code works as expected.

推荐答案

我所看到的一些事情可能是原因。

Couple of things that I have seen could possibly be the cause.

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

If you are using IE, I have seen that perform a GET immediately before doing a POST (to the same URL, with the same data being sent over), so it could be worth trying to check for that on your server (and ignore the GET)

在AJAX调用之后,你可以在按钮点击事件结束时添加以下内容(实际上,通常我会把第一行放在顶部,防止默认,返回语句显然是最后一次)...

Something else it maybe to add the following to the end of your button click events after the AJAX call (actually, normally I'd put the first line at the top with the prevent default, and the return statement obviously goes very last)...

e.stopImmediatePropagation();
return false;

这篇关于表单通过ajax POST提交两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆