我想在php中验证许多字段,所以我可以在php中使用循环来验证这些字段吗?我现在使用以下代码
if(empty($add_data['first_name']))
{
$responseJSON = array("Status" => false,"Message" => "please enter first_name");
header("content-type:application/json");
$response = json_encode($responseJSON);
echo $response;
}
elseif(empty($add_data['last_name'])))
{
$responseJSON = array("Status" => false,"Message" => "please enter your last_name");
header("content-type:application/json");
$response = json_encode($responseJSON);
echo $response;
}
...and so on...如何在循环中验证?提前感谢
发布于 2019-02-06 21:00:07
创建字段数组及其错误消息,例如
$fields = array('first_name' => "please enter first_name",
'last_name' => "please enter your last_name");然后,您可以遍历此数组以检查所有值并发出相应的错误消息:
foreach ($fields as $field => $error) {
if(empty($add_data[$field])) {
$responseJSON = array("Status" => false,"Message" => $error);
header("content-type:application/json");
$response = json_encode($responseJSON);
echo $response;
}
}https://stackoverflow.com/questions/54554167
复制相似问题