jQuery插件验证电子邮件检查是否还有疯狂

时间:2013-03-13 00:18:09

标签: php ajax validation json challenge-response

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

PHP代码:查看IFSE声明

/* 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 FORM INPUT CODE:

    <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中的2-Relevant 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-en.js中ajaxUserCallPhp的3-Relevent代码:

"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"
            },

我确定问题在于这个回声。

echo json_encode($ arrayToJs)

请帮助我花费很长时间来完成这项工作并且几乎完全正常工作。

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

非常感谢你。

2 个答案:

答案 0 :(得分:0)

好吧,小提琴后终于明白了。我发现当发布任何错误或警告时,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
  }

答案 1 :(得分:0)

我能够在不修改语言文件的情况下更改ajaxusercallphp,ajaxnamecallphp的ajax url ...你需要在jaquery.validateEngine.js中搜索这一行

查找:_ajax:function(字段,规则,I,选项)

然后向下滚动到ajax请求.ie $ .ajax

将url:rule.url更改为options.ajaxCallPhpUrl

然后你要做的就是将url包含在这样的选项中:

JQuery(“#formid”)。validateEngine('attach',{ajaCallPhpUrl:“yoururl goes here”,onValidationComplete:function(form,status){ }) 我能够在不修改语言文件的情况下更改ajaxusercallphp,ajaxnamecallphp的ajax url ...你需要在jaquery.validateEngine.js中搜索这一行

查找:_ajax:function(字段,规则,I,选项)

然后向下滚动到ajax请求.ie $ .ajax

将url:rule.url更改为options.ajaxCallPhpUrl

然后你要做的就是将url包含在这样的选项中:

JQuery(“#formid”)。validateEngine('attach',{ajaCallPhpUrl:“yoururl goes here”,onValidationComplete:function(form,status){ })