已定义的未定义变量
Undefined variable that has been defined
我正在尝试创建一个简单的联系表单,但出于某种原因,当我加载 index.php 文件时出现以下错误:
Notice: Undefined variable: emailError in C:\xampp\htdocs\Coming Soon
Landing page\index.php on line 105
这是我的代码:(重要的部分是顶部的 PHP 标签,以及 "signup" 部分 ID 中的 php 标签)
<?php
if (isset($_POST['submit'])) {
$from = $_POST['email'];
$to = 'carsonclark2009@gmail.com';
$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>
我在页面顶部定义了变量,据我所知(目前尚不清楚)它应该可以工作,但在我点击提交之前我就收到了这个错误。我想知道出了什么问题。非常感谢任何输入。
感谢
您必须在顶部定义 $emailError 变量
$emailError = "";
if (isset($_POST['submit'])) {
$from = $_POST['email'];
$to = 'carsonclark2009@gmail.com';
$subject = 'email sign up';
$message = 'please sign me up to the mailing list';
if (!$_POST['email']) {
$emailError = "Please enter a valid email Address";
}
}
?>
I defined the variable at the top of the page.
if (!$_POST['email']) {
$emailError = "Please enter a valid email Address";
}
不,您是在 if 语句中定义此变量,可能条件为假。
在 if 语句之外的顶部声明变量或使用此代码:
<?php if (!empty($emailError)) { echo $emailError;} ?>
注意:考虑替换此代码
if (!$_POST['email'])
和
if (!isset($_POST['email']))
只有当 if-condition 为真时,您的变量才会被定义。
您可以定义默认值以防止出错;
<?php
$emailError = '';
if (isset($_POST['submit'])) {
$from = $_POST['email'];
$to = 'carsonclark2009@gmail.com';
$subject = 'email sign up';
$message = 'please sign me up to the mailing list';
if (!$_POST['email']) {
$emailError = "Please enter a valid email Address";
}
}
?>
而不是
<?php echo $emailError; ?>
使用这个
<?php if(isset($emailError)){ echo $emailError;} ?>
或者只是将 @
符号放在 $emailError
变量之前以禁用该行的错误报告
首先在顶部定义$emailError
为空,这样不会给你Undefined Variable通知,比如:
<?php
$emailError = '';
// your code .....
然后您可以进一步检查是否需要设置 $emailError
,例如:
<?php
if(!empty($emailError)){
echo $emailError;
}
?>
我正在尝试创建一个简单的联系表单,但出于某种原因,当我加载 index.php 文件时出现以下错误:
Notice: Undefined variable: emailError in C:\xampp\htdocs\Coming Soon Landing page\index.php on line 105
这是我的代码:(重要的部分是顶部的 PHP 标签,以及 "signup" 部分 ID 中的 php 标签)
<?php
if (isset($_POST['submit'])) {
$from = $_POST['email'];
$to = 'carsonclark2009@gmail.com';
$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>
我在页面顶部定义了变量,据我所知(目前尚不清楚)它应该可以工作,但在我点击提交之前我就收到了这个错误。我想知道出了什么问题。非常感谢任何输入。
感谢
您必须在顶部定义 $emailError 变量
$emailError = "";
if (isset($_POST['submit'])) {
$from = $_POST['email'];
$to = 'carsonclark2009@gmail.com';
$subject = 'email sign up';
$message = 'please sign me up to the mailing list';
if (!$_POST['email']) {
$emailError = "Please enter a valid email Address";
}
}
?>
I defined the variable at the top of the page.
if (!$_POST['email']) {
$emailError = "Please enter a valid email Address";
}
不,您是在 if 语句中定义此变量,可能条件为假。
在 if 语句之外的顶部声明变量或使用此代码:
<?php if (!empty($emailError)) { echo $emailError;} ?>
注意:考虑替换此代码
if (!$_POST['email'])
和
if (!isset($_POST['email']))
只有当 if-condition 为真时,您的变量才会被定义。
您可以定义默认值以防止出错;
<?php
$emailError = '';
if (isset($_POST['submit'])) {
$from = $_POST['email'];
$to = 'carsonclark2009@gmail.com';
$subject = 'email sign up';
$message = 'please sign me up to the mailing list';
if (!$_POST['email']) {
$emailError = "Please enter a valid email Address";
}
}
?>
而不是
<?php echo $emailError; ?>
使用这个
<?php if(isset($emailError)){ echo $emailError;} ?>
或者只是将 @
符号放在 $emailError
变量之前以禁用该行的错误报告
首先在顶部定义$emailError
为空,这样不会给你Undefined Variable通知,比如:
<?php
$emailError = '';
// your code .....
然后您可以进一步检查是否需要设置 $emailError
,例如:
<?php
if(!empty($emailError)){
echo $emailError;
}
?>