php - Undefined variable that has been defined

904

I am trying to create a simple contact form and for some reason when I load my index.php file I get the following error:

Notice: Undefined variable: emailError in C:\xampp\htdocs\Coming Soon Landing page\index.php on line 105

Here is my code: (the important parts are the PHP tags at the top, and the php tags in the "signup" section ID)

<?php

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

      $from = $_POST['email'];
      $to = '[email protected]';
   $subject = 'email sign up';
   $message = 'please sign me up to the mailing list';

   if (!$_POST['email']) {
    $emailError = "Please enter a valid email Address";
   }

 }

?>

<!DOCTYPE html>
 <html lang="en">
  <head>
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
   <title>Landing Page</title>

   <!-- Bootstrap -->
   <link href="css/bootstrap.min.css" rel="stylesheet">
   <!-- Custom CSS -->
   <link rel="stylesheet" href="css/style.css">
   <!-- Fonts -->
   <link rel="stylesheet" href="font-awesome/css/font-awesome.css">
   <link href="https://fonts.googleapis.com/css?family=Just+Another+Hand" rel="stylesheet">
   <link href="https://fonts.googleapis.com/css?family=Amatica+SC" rel="stylesheet">
  </head>
  <body>

   <section id="logo">

    <div >
      <div >

       <div >
        <img src="img/my-logo.png" >
       </div>

      </div>
    </div>

   </section>


   <section id="intro">

    <div >
     <div >
      <div >
       <p>We're working hard, we'll be ready to launch in...</p>
      </div>
     </div>
    </div>

   </section>

    <section id="counter">

    <div >
     <div >
      <div >
       <div ></div>
      </div>
     </div>
    </div>

   </section>

   <section id="icons">

    <div >
     <div >
      <div >
        <ul >
          <a href="#"><li ><i aria-hidden="true"></i></li></a>
          <a href="#"><li ><i aria-hidden="true"></i></li></a>
          <a href="#"><li ><i aria-hidden="true"></i></li></a>
          <a href="#"><li ><i aria-hidden="true"></i></li></a>
        </ul>
      </div>
     </div>
    </div>

   </section>

   <section id="signup">

    <div >
     <div >

      <div >
        <form role="form" method="post" action="#signup">
          <input type="email" name="email" placeholder="enter your email">
          <button type="submit" name="submit" value="send">Find out more</button>
        </form>
       <?php echo $emailError; ?>
      </div>

     </div>
    </div>

   </section>

I defined the variable at the top of the page, and to my knowledge (which is lacking) it should be working but before I even click submit I get this error. I'm wondering what's wrong. Any input is greatly appreciated.

thanks

114

Answer

Solution:

I defined the variable at the top of the page.

if (!$_POST['email']) {
    $emailError = "Please enter a valid email Address";
}

No, you're defining this variable in an if statement, maybe condition is false.

Declare variable at the top outside if statement or use this code:

<?php if (!empty($emailError)) { echo $emailError;} ?>

Note: consider replacing this code

if (!$_POST['email'])

with

if (!isset($_POST['email']))
957

Answer

Solution:

Your variable will be defined only if if-condition will be true.

You can define default value to prevent errors;

<?php

  $emailError = '';

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

      $from = $_POST['email'];
      $to = '[email protected]';
      $subject = 'email sign up';
      $message = 'please sign me up to the mailing list';

      if (!$_POST['email']) {
        $emailError = "Please enter a valid email Address";
      }

  }

?>
892

Answer

Solution:

instead of

 <?php echo $emailError; ?>

use this

 <?php if(isset($emailError)){ echo $emailError;} ?>

or just put@ symbol before your$emailError variable to disable error reporting for that line

96

Answer

Solution:

First of all define$emailError as empty at top, this will not give you Undefined Variable notice, like:

<?php
$emailError = '';
// your code .....

And than you can further check if you need either$emailError set or not, like:

<?php 
if(!empty($emailError)){
  echo $emailError; 
}
?>

People are also looking for solutions to the problem: php - phpmailer email not received by gmail if sender is yahoo mail

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.