我有一个php简单的项目。 该项目将创建一个电子邮件提交页面。 我使用phpstorm软件。
<?php
//get user input
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
//error message
$missingName = '<p><strong>Please enter your name</strong></p>';
$missingEmail = '<p><strong>Please enter your email address</strong></p>';
$invalidEmail = '<p><strong>Please enter a valid email address</strong></p>';
$missingMessage = '<p><strong>Please enter a message</strong></p>';
//if the user has submitted the form
if ($_POST["submit"]){
//check for errors
if (!$name){
$errors .= $missingName;
}else{
filter_var($name, FILTER_SANITIZE_STRING);
}
if (!$email){
$errors .= $missingEmail;
}else{
filter_var($email, FILTER_SANITIZE_EMAIL);
if (!filter_var($invalidEmail, FILTER_VALIDATE_EMAIL)){
$errors .= $invalidEmail;
}
}
if (!$message){
$errors .= $missingMessage;
}else{
$message = filter_var($message, FILTER_SANITIZE_STRING);
}
//if there are any errors
if ($errors){
$resultMessage = '<div class="alert alert-danger">' . $errors . '</div>';
}
}
?>
<form action="index.php" method="post">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" name="name" placeholder="Name" id="name" class="form-control">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" name="email" placeholder="Email" id="email" class="form-control">
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea name="message" id="message" class="form-control" rows="5"></textarea>
</div>
<input type="submit" name="submit" id="submit" class="btn btn-success btn-lg" value="Send Message">
</form>
当我运行项目时,我遇到了您在图像中看到的错误
错误消息
错误来自以下行
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
而这条线
if ($_POST["submit"])
请帮忙!
在访问 POST 变量之前,您应该检查它是否不为空:
if(isset($_POST['submit']){
//your code
}