4

我对 php 脚本有疑问,它添加的记录比它想象的要多。我有几个“子表”的大表,其中包含人们的薪水及其分布:

姓名 # PayRate # Distribution # 年度

约翰史密斯 # 10,000 # 20.00% # 50,000 # 编辑 # 删除
彼得潘 # 100,000 # 100.00% # 100,000 # 编辑 # 删除

添加新人

当您单击新人员时,弹出表单,键入所有信息,将人员添加到数据库,ajax 返回信息并将人员添加到列表末尾。这里是问题,当我点击一个“子表”添加人员时,它添加了一个人并正确打印,但是当我转到第二个“子表”并添加一个人时,它会添加两次人员(插入两条相同的记录到数据库),如果我在第三个“子表”上单击添加人员,它将添加一条记录三次......它应该只添加一次记录。这是我的js脚本:

$('.addbtt').on('click', function(){
    value= "";
    document.getElementById("updform").reset();
    var glnumber = $(this).parent().parent().find('#glno').text();
    var objid = glnumber.substring(7,12);
    value += glnumber.substring(10,12);
    if(objid=='51001'){
        $('.modal-title').html("Add person - Full time");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51002'){
        $('.modal-title').html("Add person - Part time");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51009'){
        $('.modal-title').html("Add person - GA");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51010'){
        $('.modal-title').html("Add person - GE");
        $('#glnumber').empty().val(glnumber);
    };

    $('#updform').submit(function(){
        var formData = $(this).serialize();
        $.post('includes/updatesal.php',formData,processData);
        function processData(data){
            if(value == 01){
                $('#addperson1').before(data);
            }else if(value == 02){
                $('#addperson2').before(data);
            }else if(value == 09){
                $('#addperson3').before(data);
            }else if(value == 10){
                $('#addperson4').before(data);
            }else{
                alert("Your are doing something you shouldn't! :)") 
            };
        }; //end of processData
        return false;
    });
});

我认为问题在于我的“添加人员按钮”,因为它还向数据库添加了两条记录。这是我的php函数:

$insertSQL1 = "INSERT INTO salaries (gl_number,name,pay_rate,distribution,annual)
                VALUES ('".$glnumber."','".$name."','".$payRate."','".$dist."','".$annual."')";
        $insertSQL1 .= "; SELECT SCOPE_IDENTITY() AS RECORD_ID";    
        $stmt1 = sqlsrv_query(Database::GetInstance()->databaseConnection, $insertSQL1);
        # IF THERE IS AN ERROR
        if(!$stmt1){
            echo "Error";   
        }else{
            $id = Database::GetInstance()->LastInsertId($stmt1);
        }
        # IF ALL QUERIES WERE SUCCSESSFUL, COMMIT THE TRANSACTION, OTHERWISE ROLLBACK
        if($stmt1 && !$errors){
            sqlsrv_commit(Database::GetInstance()->databaseConnection);
            # FREE THE STATEMENT
            Database::GetInstance()->FreeDBStatement($stmt1);

我正在尝试解决这个问题一个星期,有人可以帮忙吗?

4

2 回答 2

4

问题是由您的.submit()事件处理程序位于事件处理程序内部引起的.click()——因此您.submit()每次单击都会创建一个新的、相同的处理程序。

要修复,只需将.submit()处理程序从那里取出:

$('.addbtt').on('click', function(){
    value= "";
    document.getElementById("updform").reset();
    var glnumber = $(this).parent().parent().find('#glno').text();
    var objid = glnumber.substring(7,12);
    value += glnumber.substring(10,12);
    if(objid=='51001'){
        $('.modal-title').html("Add person - Full time");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51002'){
        $('.modal-title').html("Add person - Part time");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51009'){
        $('.modal-title').html("Add person - GA");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51010'){
        $('.modal-title').html("Add person - GE");
        $('#glnumber').empty().val(glnumber);
    };
});

$('#updform').submit(function(){
    var formData = $(this).serialize();
    $.post('includes/updatesal.php',formData,processData);
    function processData(data){
        if(value == 01){
            $('#addperson1').before(data);
        }else if(value == 02){
            $('#addperson2').before(data);
        }else if(value == 09){
            $('#addperson3').before(data);
        }else if(value == 10){
            $('#addperson4').before(data);
        }else{
            alert("Your are doing something you shouldn't! :)") 
        };
    }; //end of processData
    return false;
});
于 2013-10-09T14:33:03.060 回答
0

您也可以将表单清除脚本移动到“提交处理程序”内部

// No need following on click handler, if button is submit
$('.addbtt').on('click', function(){

});

$('#updform').submit(function(){
value= "";
    document.getElementById("updform").reset();
    var glnumber = $(this).parent().parent().find('#glno').text();
    var objid = glnumber.substring(7,12);
    value += glnumber.substring(10,12);
    if(objid=='51001'){
        $('.modal-title').html("Add person - Full time");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51002'){
        $('.modal-title').html("Add person - Part time");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51009'){
        $('.modal-title').html("Add person - GA");
        $('#glnumber').empty().val(glnumber);
    }else if(objid=='51010'){
        $('.modal-title').html("Add person - GE");
        $('#glnumber').empty().val(glnumber);
    };

    var formData = $(this).serialize();
    $.post('includes/updatesal.php',formData,processData);
    function processData(data){
        if(value == 01){
            $('#addperson1').before(data);
        }else if(value == 02){
            $('#addperson2').before(data);
        }else if(value == 09){
            $('#addperson3').before(data);
        }else if(value == 10){
            $('#addperson4').before(data);
        }else{
            alert("Your are doing something you shouldn't! :)") 
        };
    }; //end of processData
    return false;
});
于 2013-10-09T14:37:13.843 回答