jQuery插件验证电子邮件检查,如果否则


jQuery plugin Validation email check if else CRAZY WIERD

我已经成功地实现了Jquery验证插件http://posabsolute.github.com/jQuery-Validation-Engine/,但我现在正试图得到一个ajax数据库电子邮件检查工作(电子邮件存在/电子邮件可用),我已经写了一些php脚本来完成这项工作。它有点工作,但我从我的IF ELSE语句中得到了最意想不到的异端奇怪行为(对我来说似乎真的很疯狂)。观察###标记的注释

PHP代码:查看IF ELSE语句
/* RECEIVE VALUE */
$validateValue = $_REQUEST['fieldValue'];
$validateId = $_REQUEST['fieldId'];

$validateError = "This username is already taken";
$validateSuccess = "This username is available";

/* RETURN VALUE */
$arrayToJs = array();
$arrayToJs[0] = $validateId;
$req = "SELECT Email
  FROM business
  WHERE Email = '$validateValue'";
$query = mysql_query($req);
while ($row = mysql_fetch_array($query)) {
  $results = array($row['Email']);
}

if (in_array($validateValue, $results)) {
   $arrayToJs[1] = false;
   echo json_encode($arrayToJs); // RETURN ARRAY WITH ERROR ### popup shows "validating, please wait" then "This username is already taken" when email typed is in database - i.e. Working
   file_put_contents('output.txt', print_r("1 in array - Email is Taken  " . $validateValue, true)); ### this runs!!
}else{
  $arrayToJs[1] = true; // RETURN TRUE
  echo json_encode($arrayToJs); // RETURN ARRAY WITH success ### popup shows "validating, please wait" when email typed is NOT in the database - i.e. not Working
  file_put_contents('output.txt', print_r("2 else - Email is available  " .   $validateValue, true)); 
  //### THIS RUNS TOO !!!!!!!!!!!!! i.e. echo json_encode($arrayToJs) wont work for both.. If I change (in_array()) to (!in_array()) i get the reverse when email is in database. 
  //i.e. only the else statements echo json_encode($arrayToJs) runs and the popup msg shows up green "This username is available" crazy right??? 
  //so basically IF ELSE statements run as expected (confirmed by output.txt) but only one echo json_encode($arrayToJs) will work.!!!! 
  //If i remove the json_encode($arrayToJs) statements and place it once after the IF ELSE statement i get the same problem.
  //both $arrayToJs[1] = false; and $arrayToJs[1] = true; can work separately depending on which is first run IF or ELSE but they will not work in the one after another;
  }

下面是剩下的代码——>

1-HTML表单输入代码:

    <tr>
        <td> <Label>Business Email</Label>
            <br>
            <input type="text" name="Email" id="Email" class="validate[required,custom[email],ajax[ajaxUserCallPhp]] text-input">
        </td>
    </tr>
JQUERY . validationengine .js中的JQUERY代码:
$.ajax({
            type: type,
            url: url,
            cache: false,
            dataType: dataType,
            data: data,
            form: form,
            methods: methods,
            options: options,
            beforeSend: function() {
                return options.onBeforeAjaxFormValidation(form, options);
            },
            error: function(data, transport) {
                methods._ajaxError(data, transport);
            },
            success: function(json) {
                if ((dataType == "json") && (json !== true)) {
                    // getting to this case doesn't necessary means that the form is invalid
                    // the server may return green or closing prompt actions
                    // this flag helps figuring it out
                    var errorInForm=false;
                    for (var i = 0; i < json.length; i++) {
                        var value = json[i];
                        var errorFieldId = value[0];
                        var errorField = $($("#" + errorFieldId)[0]);
                        // make sure we found the element
                        if (errorField.length == 1) {
                            // promptText or selector
                            var msg = value[2];
                            // if the field is valid
                            if (value[1] == true) {
                                if (msg == ""  || !msg){
                                    // if for some reason, status==true and error="", just close the prompt
                                    methods._closePrompt(errorField);
                                } else {
                                    // the field is valid, but we are displaying a green prompt
                                    if (options.allrules[msg]) {
                                        var txt = options.allrules[msg].alertTextOk;
                                        if (txt)
                                            msg = txt;
                                    }
                                    if (options.showPrompts) methods._showPrompt(errorField, msg, "pass", false, options, true);
                                }
                            } else {
                                // the field is invalid, show the red error prompt
                                errorInForm|=true;
                                if (options.allrules[msg]) {
                                    var txt = options.allrules[msg].alertText;
                                    if (txt)
                                        msg = txt;
                                }
                                if(options.showPrompts) methods._showPrompt(errorField, msg, "", false, options, true);
                            }
                        }
                    }
                    options.onAjaxFormComplete(!errorInForm, form, json, options);
                } else
                    options.onAjaxFormComplete(true, form, json, options);
            }
        });

jquery. validationengine - enen .js中ajaxUserCallPhp的相关代码:

"ajaxUserCallPhp": {
                "url": "validation/php/ajaxValidateFieldUser.php",
                // you may want to pass extra data on the ajax call
                "extraData": "name=eric",
                // if you provide an "alertTextOk", it will show as a green prompt when the field validates
                "alertTextOk": "* This username is available",
                "alertText": "* This user is already taken",
                "alertTextLoad": "*Validating, please wait"
            },

我确定问题出在这个回声上。

回声json_encode (arrayToJs美元)

请帮助我花了很长时间在这个和它几乎完全工作。

澄清-我基本上是试图编码它,以便如果我在数据库中输入电子邮件,它显示红色"此用户名已采取",然后如果我编辑输入框到电子邮件不在数据库中,它更改为绿色"用户名是可用的"目前只有一个json_encode将在任何情况下运行无论我如何改变if else语句-

非常感谢。

好了,终于弄到了。我发现json_encode()在发布任何错误或警告时返回false。使用xampp/PHP/logs/error_logs文件中的PHP错误日志文件,我意识到只有当查询结果为null时才会出现错误,从而使$results = null。这会导致一个输出错误,阻止json_encode()回显true,这就是为什么我只得到一个响应。

为了解决这个问题,我确保$result数组不是空的,在查询数组部分后使用以下代码。

if(empty($results)){
   $results [0]= ("obujasdcb8374db");
}

整个代码现在是

$req = "SELECT Email
FROM business
WHERE Email = '$validateValue'";
$query = mysql_query($req);
while ($row = mysql_fetch_array($query)) {
$results[] = $row['Email'];
}
if(empty($results)){
  $results [0]= ("obujasdcb8374db");
}
if (in_array($validateValue, $results)) {
  $arrayToJs[1] = 0;
  echo json_encode($arrayToJs); // RETURN ARRAY WITH ERROR

} else {
  $arrayToJs[1] = 1; // RETURN TRUE
  echo json_encode($arrayToJs); // RETURN ARRAY WITH success
  }

我能够改变ajax url为ajaxusercallphp, ajaxnamecallphp不修改语言文件…您需要在jquery . validateengine .js

中搜索这一行

查找:_ajax:function(field,rules,I,options)

然后滚动到ajax请求。ie $.ajax

修改url:rule。url到options.ajaxCallPhpUrl

那么你所要做的就是把url作为一个选项,像这样:

JQuery("# formid")。validateEngine('attach', {ajaCallPhpUrl: "yoururl到这里",onValidationComplete:function(form,status){})我能够改变ajax url为ajaxusercallphp, ajaxnamecallphp不修改语言文件…您需要在jquery . validateengine .js

中搜索这一行

查找:_ajax:function(field,rules,I,options)

然后滚动到ajax请求。ie $.ajax

修改url:rule。url到options.ajaxCallPhpUrl

那么你所要做的就是把url作为一个选项,像这样:

JQuery("# formid")。validateEngine('attach', {ajaCallPhpUrl: "yoururl到这里",onValidationComplete:function(form,status){})