0

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?

5
  • 2
    Show you form please. Commented Jun 10, 2015 at 7:48
  • 1
    Test the keys. $value = isset($_POST["recipient"]) ? $_POST["recipient"] : ''; (basic example) Commented Jun 10, 2015 at 7:50
  • 1
    is this a copy paste? it seems you don't have input elements in your form named as recipient, recipient0... and so on Commented Jun 10, 2015 at 7:50
  • form looks like this: jsfiddle.net/rokas_m/147j0010 Commented Jun 10, 2015 at 8:15
  • 1
    try if (!empty($_POST['recepient'])) $formproc->AddRecipient($_POST["recipient"]); Commented Jun 10, 2015 at 8:23

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1
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"]); 
} 

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.