Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 防止在数据库中输入唯一字段_Php_Oop - Fatal编程技术网

Php 防止在数据库中输入唯一字段

Php 防止在数据库中输入唯一字段,php,oop,Php,Oop,在这里,我试图使“电子邮件”和“移动”作为独特的领域。但如果一个字段有效,另一个字段无效,则意味着如果电子邮件是唯一的,而手机不是唯一的,则表单将被提交。如果两者都是唯一的,则警报正常工作。我的问题是“如果alteast一个字段不唯一,则不应提交表单”。这是代码 submitHandler: function(form) { var email = form.Email.value; var AgencyID = form.AgencyID.value

在这里,我试图使“电子邮件”和“移动”作为独特的领域。但如果一个字段有效,另一个字段无效,则意味着如果电子邮件是唯一的,而手机不是唯一的,则表单将被提交。如果两者都是唯一的,则警报正常工作。我的问题是“如果alteast一个字段不唯一,则不应提交表单”。这是代码

    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);
        }

        });



    }



});

我的意见是,最好更改代码,使用一个请求发送到服务器,并同时发送电子邮件和手机。 但只是为了修复您的代码(抱歉,我没有调试,但您必须了解):


因为您正在执行两个单独的ajax,并且都在成功提交表单。将两个检查页合并为一个,并同时执行所有检查。或者,为emailSuccess创建一个变量,并相应地在您的第一个ajax上设置它,然后删除表单。submit()然后在移动ajax成功中检查电子邮件变量,发布错误警报或提交表单,然后您可以更正代码,我对ajaxno知之甚少,对不起,这是您的工作。
    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();
        }
    }
});