Xtremer360 Posted June 21, 2011 Share Posted June 21, 2011 How should I keep this separate if only one of these fields are required. <?php if((empty($_POST['username'])) || (trim($_POST['username'])=="") || ($_POST['username'] == NULL) || (!isset($_POST['username']))){$errors = "yes";} if((empty($_POST['email'])) || (trim($_POST['email'])=="") || ($_POST['email'] == NULL) || (!isset($_POST['email']))){$errors = "yes";} // Error checking, make sure all form fields have input if ($errors == "yes") { // Not all fields were entered error $message = "You must enter a value for either the username or email address!"; $output = array('errorsExist' => true, 'message' => $message); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240017-must-fill-out-one-of-two-fields/ Share on other sites More sharing options...
freelance84 Posted June 21, 2011 Share Posted June 21, 2011 ie: will pass if username or email exists? Quote Link to comment https://forums.phpfreaks.com/topic/240017-must-fill-out-one-of-two-fields/#findComment-1232919 Share on other sites More sharing options...
Pikachu2000 Posted June 21, 2011 Share Posted June 21, 2011 Using so many comparisons is unnecessary. Form fields are all sent by default as string data, so they will either be a string, an empty string, or not present in the $_POST array at all (for some field types such as unchecked checkboxes). if( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) { // form has been submitted ) { $username = trim($_POST['username']); $email = trim($_POST['email']); if( empty($username) && empty($email) ) { // IF BOTH FIELDS ARE EMPTY, ERROR CONDITION EXISTS $message = "You must enter a value for either the username or email address!"; } } echo $message; ?> Quote Link to comment https://forums.phpfreaks.com/topic/240017-must-fill-out-one-of-two-fields/#findComment-1232920 Share on other sites More sharing options...
Xtremer360 Posted June 21, 2011 Author Share Posted June 21, 2011 Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/240017-must-fill-out-one-of-two-fields/#findComment-1232929 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.