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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.