Search code examples
phpforum

PHP code to validate that the user filled out atleast 1 field


I have a forum where the user can choose which column he or she would like to fill in.

I want to not require each box, but only require that he or she fills out one of the boxes.

Here is a picture of what the forum looks like: enter image description here

They can choose to enter information into "Sidewalks and non-roadway areas," or "Parking (no meters)." It is not required to fill all the fields, but just one.

My code seems to work, except it won't spit out the error if the user does not fill out a field. Here is my code:

<?php
$var_all = FALSE;        // all variable

     // Fees Validation
 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['sidewalk_days_close'])) {
        $var_sidewalk_days_closed = TRUE;
            $var_all = TRUE;
        $fee_SDC = trim($_POST['sidewalk_days_close']);
        }
    if (isset($_POST['parking_noMeters_spaces'])) {
        $var_parking_noMeters_spaces = TRUE;
        $var_all = TRUE;
        $fee_PNMS = trim($_POST['parking_noMeters_spaces']);
        }
    if (isset($_POST['parking_noMeters_days'])) {
        $var_parking_noMeters_days = TRUE;
        $var_all = TRUE;
        $fee_PNMD = trim($_POST['parking_noMeters_days']);
        }
    if (isset($_POST['parking_Meters_spaces'])) {
        $var_parking_meters_with_spaces = TRUE;
        $var_all = TRUE;
        $fee_PMS = trim($_POST['parking_Meters_spaces']);
        }
    if (isset($_POST['parking_Meters_days'])) {
        $var_parking_meters_days = TRUE;
        $var_all = TRUE;
        $fee_PMD = trim($_POST['parking_Meters_days']);
        }
    if (isset($_POST['vehicle_lane_close'])) {
        $var_vehicle_lane_close = TRUE;
        $var_all = TRUE;
        $fee_VLC = trim($_POST['vehicle_lane_close']);
        }
    if (isset($_POST['alley_closure'])) {
        $var_alley_closure = TRUE;
        $var_all = TRUE;
        $fee_AC = trim($_POST['alley_closure']); 
        }
 }

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{ 
    if($var_all == FALSE)
    {
        $problem = TRUE;
        print '<div id="fee_error">Please fill in one of the fields below</div>';
    }
    else
    {
        print 'this is text';
    }
}
?>

Solution

  • Here's the corrected code

    <?php
    
    $var_all = FALSE;        // all variable
    
    // Fees Validation
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        if (isset($_POST['sidewalk_days_close']))
        {
            $var_sidewalk_days_closed = TRUE;
            $var_all = TRUE;
            $fee_SDC = trim($_POST['sidewalk_days_close']);
        }
    
        if (isset($_POST['parking_noMeters_spaces']))
        {
            $var_parking_noMeters_spaces = TRUE;
            $var_all = TRUE;
            $fee_PNMS = trim($_POST['parking_noMeters_spaces']);
        }
    
        if($var_all == FALSE)
        {
            $problem = TRUE;
            print '<div id="fee_error">Please fill in one of the fields below</div>';
        }
        else
        {
            print 'this is text';
        }
    }
    ?>