【发布时间】:2017-05-18 17:49:33
【问题描述】:
我遇到了一个奇怪的问题,我似乎无法正确调试。 我目前正在使用 AngularJS 和对 PHP 文件的 JSON 请求创建一个注册表单。这是我用于调试的输入数据示例。
现在发送的请求清楚地包含以下数据,如调试 JSON 响应所见。
现在由于某种原因,$response['debug-two'] 或 $response['debug-info-two'] 不会添加到响应的 JSON 中,即使所需的所有字段都是 !empty() 或 true。此外,$reponse['empty-fields'] 也不是。因此,它必须真正归结为空字段 if 语句,但我似乎无法理解或弄清楚原因,因为所有字段都设置为 $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);
【问题讨论】:
-
这一行有错字;
$reponse['empty-fields'] = true;- 应该是$response['empty-fields'] = true;(你错过了's')。你到底想用 if 语句检查什么? -
@thebluefox 哦,是的,这让空白字段消失了,但为什么会这样呢?当所有字段都是 !empty() 并且协议 1 为真时?
-
做一点调试,看看哪些表达式是错误的。
var_dump((!empty($fullname)) . ' && ' .(!empty($email)) . ' && ' .(!empty($birthday)) . ' && ' .(!empty($password1)) . ' && ' .(!empty($password2)) . ' && ' .($agreement1) ); -
@thebluefox 找到问题了,谢谢! :)
标签: php angularjs json if-statement