如果输入不为空,则不显示

时间:2015-03-23 21:08:26

标签: javascript php json

如果用户没有输入用户名,但是当用户输入显示"username": null的用户名时,我会显示错误消息。当没有我试图在数组中添加if语句时,我怎么也不能显示任何错误

$username = $_POST['username'];
if(empty($username)){
    $username_error = "Please enter a Username";
}
  $json = array(
        'ErrorMsg' => array( 

         'username' => $username_error,
        ),
        'booleanFlag' => TRUE,    
    );

1 个答案:

答案 0 :(得分:0)

您需要为$ username_error提供备用值(空白):

$username = $_POST['username'];
if(empty($username)){
    $username_error = "Please enter a Username";
} else {
    $username_error = "";
}
$json = array(
    'ErrorMsg' => array( 
       'username' => $username_error,
     ),
    'booleanFlag' => TRUE,    
);