我是一名前端开发人员,我是PHP的新手。我使用BootStrap3为我的网站构建联系表单,而表单实际上提交了邮件,我能够在我的Gmail收件箱中收到已发送的邮件,一些PHP标签显示为纯文本。模态形式。如何使错误消息显示为文本,而不是使用PHP标记污染表单?
Boottrap3表格:
<form class="form-horizontal" role="form" method="post" action="index.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?><!-- shows all the time as code-->
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?><!-- shows all the time as code-->
</div>
</div>
</form>
PHP代码(index.php)
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'demo@test.com';
$subject = 'Message from Contact Demo ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// If there are no errors, send the email
if (!$errName || !$errEmail || !$errMessage || !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
答案 0 :(得分:1)
在显示包含错误消息的段落之前添加至少if
条件,例如:
<?php if ($errName) : ?>
<p class="text-danger"><?php echo $errName ?></p>
<?php endif; ?>
它应该在你的例子中做到。
btw:您的联系表单应作为PHP文件提供。如果它是一个HTML文件,它就不会工作,因为它不会被PHP解释。
答案 1 :(得分:0)
试试这个
<?php echo "<p class='text-danger'>".$errName."</p>";?>