0

Jquery検証プラグインhttp://posabsolute.github.com/jQuery-Validation-Engine/を正常に実装しましたが、ajaxデータベースの電子メールチェックを機能させようとしています(電子メールが存在する/電子メールが利用可能)。これを行うためのphpスクリプト。それはちょっと機能していますが、IF ELSEステートメントから最も予想外の異端的に奇妙な動作をしています(私には本当にクレイジーに見えます)。###マークされたコメントを観察する

PHPコード:IFELSEステートメントを見てください

/* 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>

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

            }
        });

3-jquery.validationEngine-en.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"
            },

問題はこのエコーにあると確信しています。

echo json_encode($ arrayToJs)

私がこれに長く費やし、ほぼ完全に機能しているのを手伝ってください。

明確にするために-私は基本的にそれをコーディングしようとしています。データベースに電子メールを入力すると「このユーザー名が取得されました」と赤で表示され、データベースにない電子メールに入力ボックスを編集すると緑の「ユーザー名は現時点では、if elseステートメントをどのように変更しても、どのシナリオでも1つのjson_encodeのみが実行されます– </ p>

事前にどうもありがとうございました。

4

2 に答える 2

0

わかりました、フィドルの後、ついにそれを手に入れました。エラーまたは警告が投稿されると、json_encode() が false を返すことがわかりました。xampp/php/logs/error_logs ファイルの php エラー ログ ファイルを使用すると、クエリ結果が null の場合にのみエラーが発生し、$results = null になることがわかりました。これにより、json_encode() が true をエコーするのを妨げる出力エラーが発生しました。これが、1 つの応答しか得られなかった理由です。

それを修正するために、配列部分へのクエリの後に次のコードを使用して、 $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
  }
于 2013-03-13T18:16:23.393 に答える