-1

I am validating some input fields before sending the email. I am using for each to loop through the array faster and check that every single input is not empty and return it as a response in jquery to show the errors. The problem is that email and message inputs are not being validated. Emails are being sent even if the inputs are empty.

the array elements come from the input name attributes from the html.

function e_($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

#required fields
$required = array('name', 'email','lname','message','country' , 'city' ,'adcategory','plan' ,'company');
$success = false;

#non required fields
$website = e_($_POST['website']);
$addr = e_($_POST['address']);

foreach($required as $field) {
    if (empty($_POST[$field]))
    {
        $success = false;
    }
    else if(!empty($_POST[$field])){
        $success = true;
        $name = e_($_POST['fname']);
        $email = e_($_POST['email']); #this has issue
        $lname = e_($_POST['lname']);
        $msg = e_($_POST['message']); #this has issue

        $country = e_($_POST['country']);
        $city = e_($_POST['city']);
        $adCategory = e_($_POST['adcategory']);
        $plan = e_($_POST['plan']);
        $companyName = e_($_POST['company']);
    }

}

if($success)        
    echo "success";
else if (!$success)
    echo json_encode(['errors'=>true]); #this will be manipulated in jquery
csandreas1
  • 2,026
  • 1
  • 26
  • 48
  • An [all-in-one sanitize function](https://stackoverflow.com/questions/4223980/the-ultimate-clean-secure-function) is not ideal and it isn't recommended either. – Script47 Aug 22 '18 at 22:18

2 Answers2

2

The problem is that you set $success = true; whenever you find a required field, and this undoes the $success = false; for a previous field. You also process all the fields in the else if, even though that just means that one of the required fields was found.

$success = true;
foreach ($required as $field) {
    if (empty($_POST[$field])) {
        $success = false;
        $missing_field = $field;
        break;
    }
}

if (!$success) {
    echo json_encode(['errors'=>true, 'missing' => $missing_field]);
    exit();
}

$name = e_($_POST['fname']);
$email = e_($_POST['email']); #this has issue
$lname = e_($_POST['lname']);
$msg = e_($_POST['message']); #this has issue

$country = e_($_POST['country']);
$city = e_($_POST['city']);
$adCategory = e_($_POST['adcategory']);
$plan = e_($_POST['plan']);
$companyName = e_($_POST['company']);
echo "Success";
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Your foreach loop is wrong. You have your if statement that checks if it's not empty inside your for loop that checks if it's empty. You need to check to see if all the values are empty first then run that if statement.

$success = true;
foreach($required as $field) {
    if (empty($_POST[$field]))
    {
        $success = false;
        break;
    }
}

if($success)
{
    // set your variables
} else {
    // don't set your variables
}
Chad K
  • 832
  • 1
  • 8
  • 19