php - Undefined variable that has been defined
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
Answer
Solution:
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:
Note: consider replacing this code
with
Answer
Solution:
You must define $emailError variable at top
Answer
Solution:
Your variable will be defined only if if-condition will be true.
You can define default value to prevent errors;
Answer
Solution:
instead of
use this
or just put
@
symbol before your$emailError
variable to disable error reporting for that lineAnswer
Solution:
First of all define
$emailError
as empty at top, this will not give you Undefined Variable notice, like:And than you can further check if you need either
$emailError
set or not, like: