2017-01-04 11 views
2

私は正しくデバッグできないような奇妙な問題に遭遇しました。 AngularJSとJSONリクエストをPHPファイルに使用して登録フォームを作成しています。次に、デバッグに使用した入力データの例を示します。PHP if文を使って!empty()が既存の変数を正しくチェックしていない

Registration form

JSONレスポンスをデバッグすることで見られるように今はっきりと送信されたリクエストは、以下のデータが含まれています。何らかの理由で今

Debug 1 Debug 2

$response['debug-two']または$response['debug-info-two']は、必要なすべてのフィールドが!empty()またはtrueであっても、応答JSONに追加されません。また、$reponse['empty-fields']もありません。そのため、実際にはif-statementという空のフィールドに沸騰する必要がありますが、すべてのフィールドが$response['debug-info']として設定されているため、理解できない、または理由を把握できないようです。

# Prevent XSRF 
if ($session->checkXSRF()) { 
    # Get POST data 
    $data = json_decode(file_get_contents("php://input")); 

    $fullname = $data->fullname; 
    $email = $data->email; 
    $birthday = $data->bithday; 
    $password1 = $data->password1; 
    $password2 = $data->password2; 
    $agreement1 = $data->agreement1; 
    $agreement2 = $data->agreement2; 

    $response['debug'] = $data; 
    $response['debug-info'] = $fullname.$email.$birthday.$password1.$password2.$agreement1.$agreement2; 

    # Check if empty fields 
    if ((!empty($fullname)) && (!empty($email)) && (!empty($birthday)) && (!empty($password1)) && (!empty($password2)) && ($agreement1)) { 
     $response['debug-two'] = $data; 
     $response['debug-info-two'] = $fullname.$email.$birthday.$password1.$password2.$agreement1.$agreement2; 
    } else { 
     # All fields are required 
     $reponse['empty-fields'] = true; 
    } 
} else { 
    # XSRF Error detected 
    $response['xsrf-invalid'] = true; 
} 

# Return JSON response 
echo json_encode($response); 
+1

この行にはタイプミスがあります。 $ response ['empty-fields'] = true; '(あなたは 's'を忘れてしまった)' $ reponse ['empty-fields'] = true; if文で実際に何を確認しようとしていますか? – Tom

+0

@thebluefoxああ、そう、空のフィールドが消えてしまったのですが、それはなぜですか?すべてのフィールドが!empty()で、agreement1がtrueの場合は? – PhyCoMath

+1

少しデバッグを行い、それらの式のどれが偽であるかを確認します。 '&& '。(!empty($ password1))' &&'。(&空の($誕生日))。 '&&'。(!empty($ password2))。 '&&'。($ agreement1)); – Tom

答えて

1

あなたにはいくつかの問題が発生しています。

最初は$birthday = $data->bithday;である - あなたは「誕生日」第二に

から「r」を逃し、$reponse['empty-fields'] = true;であなたは「応答」から「s」を逃しました。

# Prevent XSRF 
if ($session->checkXSRF()) { 
    # Get POST data 
    $data = json_decode(file_get_contents("php://input")); 

    $fullname = $data->fullname; 
    $email = $data->email; 
    $birthday = $data->birthday; 
    $password1 = $data->password1; 
    $password2 = $data->password2; 
    $agreement1 = $data->agreement1; 
    $agreement2 = $data->agreement2; 

    $response['debug'] = $data; 
    $response['debug-info'] = $fullname.$email.$birthday.$password1.$password2.$agreement1.$agreement2; 

    # Check if empty fields 
    if ((!empty($fullname)) && (!empty($email)) && (!empty($birthday)) && (!empty($password1)) && (!empty($password2)) && ($agreement1)) { 
     $response['debug-two'] = $data; 
     $response['debug-info-two'] = $fullname.$email.$birthday.$password1.$password2.$agreement1.$agreement2; 
    } else { 
     # All fields are required 
     $response['empty-fields'] = true; 
    } 
} else { 
    # XSRF Error detected 
    $response['xsrf-invalid'] = true; 
} 

# Return JSON response 
echo json_encode($response); 
関連する問題