If condition doesn't allow to leave empty fields

240 Views Asked by At

I have the contact form, where you can add multiple recipients, but when i uploaded it on a server, there was these errors:

Notice: Undefined index: recipient in /var/www/vhosts/partizanas.lt/httpdocs/brief/contactform.php on line 17 Notice: Undefined index: recipient0 in /var/www/vhosts/partizanas.lt/httpdocs/brief/contactform.php on line 18 Notice: Undefined index: recipient1 in /var/www/vhosts/partizanas.lt/httpdocs/brief/contactform.php on line 19 Notice: Undefined index: recipient2 in /var/www/vhosts/partizanas.lt/httpdocs/brief/contactform.php on line 20 Notice: Undefined index: recipient3 in /var/www/vhosts/partizanas.lt/httpdocs/brief/contactform.php on line 21 Notice: Undefined index: email in /var/www/vhosts/partizanas.lt/httpdocs/brief/contactform.php on line 22"

i wrote the if condition which fixes the errors:

if (isset($_POST['Submit']))    
{    

$formproc->AddRecipient($_POST["recipient"]); 
$formproc->AddRecipient($_POST["recipient0"]); 
$formproc->AddRecipient($_POST["recipient1"]); 
$formproc->AddRecipient($_POST["recipient2"]); 
$formproc->AddRecipient($_POST["recipient3"]);  
$formproc->AddRecipient($_POST["email"]); 
} 

but the problem is, the form isnt sent if there are empty input fields. And i want it to send the form when not all the fields are filled.

Any advice how to do it?

3

There are 3 best solutions below

0
On
if (isset($_POST['Submit']))    
{    
if(isset($_POST["recipient"]) && $_POST["recipient"] !="" ){$formproc->AddRecipient($_POST["recipient"]);}
if(isset($_POST["recipient1"]) && $_POST["recipient"] !="" ){$formproc->AddRecipient($_POST["recipient1"]);}
if(isset($_POST["recipient2"]) && $_POST["recipient"] !="" ){$formproc->AddRecipient($_POST["recipient2"]);}
if(isset($_POST["recipient3"]) && $_POST["recipient"] !="" ){$formproc->AddRecipient($_POST["recipient3"]);}
$formproc->AddRecipient($_POST["email"]); 
} 
0
On

Check this answer

Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset() language construct can be used to detect if a variable has been already initialized.

0
On

Consider the following code to solve your problem:

if (isset($_POST['Submit']))    
{    

    if(!empty($_POST["recipient"])) {
        $formproc->AddRecipient($_POST["recipient"]); 
    }

    if(!empty($_POST["recipient0"])) {
        $formproc->AddRecipient($_POST["recipient0"]); 
    }

    if(!empty($_POST["recipient1"])) {
        $formproc->AddRecipient($_POST["recipient1"]); 
    }

    if(!empty($_POST["recipient2"])) {
        $formproc->AddRecipient($_POST["recipient2"]); 
    }

    if(!empty($_POST["recipient3"])) {
        $formproc->AddRecipient($_POST["recipient3"]);  
    }

    if(!empty($_POST["email"])) {
        $formproc->AddRecipient($_POST["email"]); 
    }
}

The idea here is to not execute the function AddRecipient() IF the input field is empty. !empty() function does that job for you.