私は正しくデバッグできないような奇妙な問題に遭遇しました。 AngularJSとJSONリクエストをPHPファイルに使用して登録フォームを作成しています。次に、デバッグに使用した入力データの例を示します。PHP if文を使って!empty()が既存の変数を正しくチェックしていない
JSONレスポンスをデバッグすることで見られるように今はっきりと送信されたリクエストは、以下のデータが含まれています。何らかの理由で今
、$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);
この行にはタイプミスがあります。 $ response ['empty-fields'] = true; '(あなたは 's'を忘れてしまった)' $ reponse ['empty-fields'] = true; if文で実際に何を確認しようとしていますか? – Tom
@thebluefoxああ、そう、空のフィールドが消えてしまったのですが、それはなぜですか?すべてのフィールドが!empty()で、agreement1がtrueの場合は? – PhyCoMath
少しデバッグを行い、それらの式のどれが偽であるかを確認します。 '&& '。(!empty($ password1))' &&'。(&空の($誕生日))。 '&&'。(!empty($ password2))。 '&&'。($ agreement1)); – Tom