开发者

Find out which the user filled out

I'm trying to figure out how to find out where I need to do the steps if it was the username or if it was the email address the user filed out.

// Assign variable values if there is values
if ((isset($_POST['username'])) && ($_POST['username'] !== NULL) && (!empty($_POST['username']))) { $username = trim($_POST['username']); }
if ((isset($_POST['email'])) && ($_POST['email'] !== NULL) && (!empty($_POST['email']))) { $email = trim($_POST['email']); }


// IF BOTH FIELDS ARE EMPTY, ERROR CONDITION EXISTS
if (empty($username) && empty($email)) { 

    $errors = "yes";
    $message = "You must enter a value for either the username or email address!";

    $output = array('errorsExist' => true, 'message' => $message);

} else if (!empty($username) && !empty($email)) {

    $errors = "yes";
    $message = "You can onl开发者_Go百科y enter a value for the username or email address!";

    $output = array('errorsExist' => true, 'message' => $message);        

} else {

}


// Assign variable values if there is values
if ((isset($_POST['username'])) && ($_POST['username'] !== NULL) && (!empty($_POST['username']))) { $username = trim($_POST['username']); }
if ((isset($_POST['email'])) && ($_POST['email'] !== NULL) && (!empty($_POST['email']))) { $email = trim($_POST['email']); }


// IF BOTH FIELDS ARE EMPTY, ERROR CONDITION EXISTS
if (empty($username) && empty($email)) { 

    $errors = "yes";
    $message = "You must enter a value for either the username or email address!";

    $output = array('errorsExist' => true, 'message' => $message);

} else if (!empty($username) && !empty($email)) {

    $errors = "yes";
    $message = "You can only enter a value for the username or email address!";

    $output = array('errorsExist' => true, 'message' => $message);        

} else {
   if(!empty($username)) {
        //Do some things if the user entered only the username
   }
   else {
        //Do some things if the user entered only email
   }
}


else if ( empty( $username ) ) {
    // Output username error
}
else if ( empty( $email ) ) {
    // Output email error
}

In this case, however, I would skip the if/else statements, and just use an error condition:

$is_error = false;
if ( empty( $username ) ) {
    $is_error = true;
    $error_messages[] = 'Your username error message';
}
if ( empty( $email ) ) {
    $is_error = true;
    $error_messages[] = 'Your email error message';
}

if ( $is_error ) {
    // Output all error messages
}
else {
    // Perform success event
}


I think you need to do your steps in the last else that will execute only if neither username and email are empty or inputted. So in the last else, you can do something like

if (!empty($username)) {
} else {
}

On another note, I think you do not need to all the 3 checks when populating $username or $email; the first and the last should suffice, like:

if (isset($_POST['username']) && !empty($_POST['username']) {
    $username = $_POST['username'];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜