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); } ?> 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? 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; ?> 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. 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
Archived
This topic is now archived and is closed to further replies.