-2

I am trying to make a php contact form but i receive the following message:

Notice: Undefined variable: errors in C:\wamp\www\contactform_test\index.php on line 45

my line 45 looks like this:

if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
        $errors .=$invalidEmail;
    }

Anyone with an answer?

chris85
  • 23,846
  • 7
  • 34
  • 51
Hans01
  • 5
  • 2

7 Answers7

0
if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
        $errors .=$invalidEmail;
    }

you have used . operator which is used to concatenate but you never declared $errors before.

$errors = "";  //add this line before if checking
Asif Rahaman
  • 775
  • 6
  • 10
0

Define the variable $error before use.

$errors = '';// or at the top of page before you assign any value to this `variable`

We should declare every variable which we are going to use in code to avoid such errors

 if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
            $errors .=$invalidEmail;
        }
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
0

PHP show the Undefined variable if you are not initialized the variable before its call. You should initialized the variable before start the scope.

if there is conditional structure and looping structure for global variable you should call and initialized the variable first

$error ='';

if(something conditional){
    $error = 'some message' or $error .="something";
}

if looping structure 

$error ='';

for and foreach and while loop{

   $error = 'some message' or $error .="something";
}

the main reason if you use the RAW PHP code is would be better to call and initialize the variable at start of PHP script such as

<?php
  $error='';


if(something conditional){
    $error = 'some message' or $error .="something";
}

 ?>

the other reason is in your syntax, you contact the string message with $error variable, which has no global defination and initialization before the IF statement and string concatenation. that why it show the undefined variable.It would be safe call and initialized the variable before it's scope of using.

Tariqul Islam
  • 267
  • 2
  • 9
0

There are two variable used in the line 45.

  $errors=" ";
  $invalidEmail="any error message";

use code before your if condition.

Jees K Denny
  • 531
  • 5
  • 27
0

I will add an alternate just for the heck of it that doesn't necessarily require an $error variable to be set previously. This option is to gather the errors into an array:

if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
    $errors[]  =  $invalidEmail;
}

To output all errors, use implode():

if(!empty($errors))
    echo implode('<br />',$errors);
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
0

I remove the . operators, and that helped!

but i receive a error message on my form.

my complete script looks like this:

 <?php
        $name = (!isset($_POST["name"]));
        $email = (!isset($_POST["email"]));
        $message = (!isset($_POST["message"]));
        $missingname = '<p><strong>Venligst indtast dit navn:</strong></p>';
        $missingemail = '<p><strong>Venligst indtast din email:</strong></p>';
        $invalidEmail = '<p><strong></strong>Venligst indtast en valid email adresse:</p>';
        $missingmessage = '<p><strong>Venligst indtast en besked:</strong></p>';

    if(!isset($_POST['submit'])) {

        if(!$name) {
            $errors = $missingname;
    }else{
        $name = filter_var($name, FILTER_SANITIZE_STRING);


    if(!$email){

        $errors =$missingemail;
    }else{
        $email = filter_var($email, FILTER_SANITIZE_EMAIL);

    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $errors =$invalidEmail;
    }

    }
    if(!$message){
        $errors =$missingmessage;
        }else {
            $message = filter_var($message, FILTER_SANITIZE_STRING);
        }
     if($errors) {
         $resultMessage ='<div class="alert alert-danger">' . $errors . '</div>';
          }else {
              $to = "xxxxx@mail.com";
              $subject = "Kontakt";
              $message  = "<p>Navn: $name.</p>
                            <p>Email: $email.</p>
                            <p>Besked:</p>
                            <p><strong>$message</strong></p>";
              $headers ="Content-type: text/html";
              if(mail($to, $subject, $message, $headers)){

                  $resultMessage ='<div class="alert alert-success"> Tak for din mail, vi vender tilbage snarest med et svar.  </div>';
          }else{

              $resultMessage ='<div class="alert alert-warning">Din mail blev ikke sendt! Prøv igen senere..</div>';

          }

          }
     echo $resultMessage;
    }

    }

?>

Hans01
  • 5
  • 2
0

Actually, your variable $email is not set that's why it is giving the error Undefined variable. what you can do is, you need to add the check which make sure that your variable is defined.

Their are variety of ways you can put the check, like following,

  1. By using isset function

    if(isset($email)){ // ... ur code }

  2. By simply added the variable in the if condition.

    if($email){ // ... ur code }

  3. You can simply set the variable to some value before if condition.

    $email = '';

Mr.Shakti
  • 1
  • 1