我正在尝试创建一个简单的联系表单,出于某种原因,当我加载index.php文件时,我收到以下错误:
注意:未定义的变量:C:\ xampp \ htdocs \中的emailError很快就会出现 第105行登陆页面\ index.php
这是我的代码:(重要的部分是顶部的PHP标记,以及"注册"部分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>
我在页面顶部定义了变量,据我所知(缺少)它应该可以工作但在我点击提交之前我得到了这个错误。我想知道出了什么问题。任何输入都非常感谢。
谢谢
答案 0 :(得分:2)
我在页面顶部定义了变量。
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']))
答案 1 :(得分:0)
您必须在顶部定义$ 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";
}
}
?>
答案 2 :(得分:0)
仅当 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";
}
}
?>
答案 3 :(得分:0)
而不是
<?php echo $emailError; ?>
使用此
<?php if(isset($emailError)){ echo $emailError;} ?>
或只在您的@
变量前添加$emailError
符号,以禁用该行的错误报告
答案 4 :(得分:0)
首先将$emailError
定义为空的顶部,这不会给你未定义的变量通知,如:
<?php
$emailError = '';
// your code .....
并且您可以进一步检查是否需要$emailError
设置,例如:
<?php
if(!empty($emailError)){
echo $emailError;
}
?>