skip to Main Content

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:xampphtdocsComing Soon
Landing pageindex.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 class="container">
            <div class="row">

              <div class="col-md-12">
                <img src="img/my-logo.png" class="img-fluid">
              </div>

            </div>
        </div>

      </section>


      <section id="intro">

        <div class="container">
          <div class="row">
            <div class="col-md-12">
              <p>We're working hard, we'll be ready to launch in...</p>
            </div>
          </div>
        </div>

      </section>

        <section id="counter">

        <div class="container">
          <div class="row">
            <div class="col-md-12">
              <div class="countDown"></div>
            </div>
          </div>
        </div>

      </section>

     <section id="icons">

        <div class="container">
          <div class="row">
            <div class="col-md-12">
                <ul class="list-inline">
                    <a href="#"><li class="list-inline-item"><i class="fa twitter fa-twitter-square fa-3x" aria-hidden="true"></i></li></a>
                    <a href="#"><li class="list-inline-item"><i class="fa facebook fa-facebook-square fa-3x" aria-hidden="true"></i></li></a>
                    <a href="#"><li class="list-inline-item"><i class="fa google fa-google-plus-square fa-3x" aria-hidden="true"></i></li></a>
                    <a href="#"><li class="list-inline-item"><i class="fa instagram fa-instagram fa-3x" aria-hidden="true"></i></li></a>
                </ul>
            </div>
          </div>
        </div>

      </section>

      <section id="signup">

        <div class="container">
          <div class="row">

            <div class="col-md-12">
                <form class="form-inline" role="form" method="post" action="#signup">
                    <input type="email" class="form-control form-control-sm" name="email" placeholder="enter your email">
                    <button type="submit" class="btn btn-signup btn-sm" 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

5

Answers


  1. You must define $emailError variable at top

     $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";
      }
    
    }
    
    ?>
    

    Login or Signup to reply.
  2. 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']))
    
    Login or Signup to reply.
  3. 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";
          }
    
      }
    
    ?>
    
    Login or Signup to reply.
  4. 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

    Login or Signup to reply.
  5. 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; 
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search