Jump to content

Must fill out one of two fields


Xtremer360

Recommended Posts

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

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;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.