防止将唯一字段输入数据库

时间:2015-01-17 14:14:42

标签: php oop

我在这里尝试制作电子邮件" &安培; "移动"作为独特的领域。但是,如果1个字段正在工作,则其他字段无效,则表示如果电子邮件是唯一的而移动设备不是,则表单将被提交。如果两者都是唯一的,则警报正常工作。我的问题是"如果更改一个字段不是唯一的"表单不应该被提交。这是代码......

    submitHandler: function(form) {



       var email = form.Email.value;
        var AgencyID = form.AgencyID.value;

        $.ajax({
        url:"http://localhost/trans/admin/checkemail.php?email="+email,
        type:"post",
        dataType:"json",
        success:function(data){
            if(data.length>0 && !AgencyID){
                alert("Email Already Exists");
            }else{
                form.submit();
            }
        },
        error: function(xhr, textStatus, errorThrown){
             alert('request failed'+errorThrown);
        }

        });

        //mobile..................................................

        var mobile = form.Mobile.value;
        var AgencyID = form.AgencyID.value;

        $.ajax({
        url:"http://localhost/trans/admin/checkmobile.php?mobile="+mobile,
        type:"post",
        dataType:"json",
        success:function(data){
            if(data.length>0 && !AgencyID){
                alert("Mobile number Already Exists");
            }else{
                form.submit();
            }
        },
        error: function(xhr, textStatus, errorThrown){
             alert('request failed'+errorThrown);
        }

        });



    }



});

1 个答案:

答案 0 :(得分:0)

我认为最好更改代码以使用一个请求到服务器并在那里发送电子邮件和移动设备的值。 但只是修复你的代码(对不起,我没有调试,但你必须得到一个想法):

    submitHandler: function(form) {
       var email = form.Email.value;
        var AgencyID = form.AgencyID.value;
var checkOK = 'TRUE';
        $.ajax({
        url:"http://localhost/trans/admin/checkemail.php?email="+email,
        type:"post",
        dataType:"json",
        success:function(data){
            if(data.length>0 && !AgencyID){
                alert("Email Already Exists");
                checkOK = 'FALSE';
            }
        },
        error: function(xhr, textStatus, errorThrown){
             alert('request failed'+errorThrown);
        }

        });

        //mobile..................................................

        var mobile = form.Mobile.value;
        var AgencyID = form.AgencyID.value;

        $.ajax({
        url:"http://localhost/trans/admin/checkmobile.php?mobile="+mobile,
        type:"post",
        dataType:"json",
        success:function(data){
            if(data.length>0 && !AgencyID){
                alert("Mobile number Already Exists");
                checkOK = 'FALSE';
            }
        },
        error: function(xhr, textStatus, errorThrown){
             alert('request failed'+errorThrown);
        }
        });
        if (checkOK == 'TRUE') {
            form.submit();
        }
    }
});