如何在我的 HTML 页面上回显此 PHP 'Field Required' 消息?
How Can I Echo This PHP 'Field Required' Message On My HTML Page?
我正在构建一个 PHP 表单。该脚本循环遍历表单字段并告诉我它们是否为空。当我提交表单时,它会将我从 index.html 重定向到 contact.php 并显示一个空白页面。我想在 index.html 中的表格上方显示 $errmsg。我究竟做错了什么?这是我从中提取信息的地方:http://www.w3schools.com/php/php_form_required.asp
PHP:
<?php
$fields = array('name', 'email', 'telephone');
$error = false;
foreach($fields AS $fieldname) {
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
$errmsg = 'Please Fill All Fields. '.$fieldname.' is empty!<br />';
$error = true;
}
}
HTML:
<form class="form-horizontal" role="form" method="post" action="contact.php">
<div class="form-group">
<span class="error">* <?php echo $errmsg;?></span>
这是我的全部 contact.php 以防问题进一步恶化。
<?php
$fields = array('name', 'email', 'telephone');
$error = false;
foreach($fields AS $fieldname) {
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
$errmsg = 'Please Fill All Fields. '.$fieldname.' is empty!<br />';
$error = true;
}
}
if(!$error) {
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$message = $_POST['message'];
$headers = "From: contact@example.com\r\n";
$to = 'me@example.com';
$subject = 'New Lead';
$body = "From: $name\n E-Mail: $email\n Phone: $telephone\n Message: $message";
mail('me@example.com', $subject, $body, $headers);
header("Location: http://www.example.com/messagesent.html");
die();
}
}
?>
编辑:整个 HTML 表格。它嵌套在 Bootstrap 对不起意大利面
<!-- Contact Form -->
<form class="form-horizontal" role="form" method="post" action="contact.php">
<div class="form-group">
<span class="error">* <?php echo $errmsg;?></span>
<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" placeholder="First & Last Name">
</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" placeholder="example@domain.com">
</div>
</div>
<div class="form-group">
<label for="telephone" class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="telephone" name="telephone" placeholder="250-555-555">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
</div>
</form>
<!-- End Contact Form -->
您不能 运行 PHP 在 html 文件中编写代码,除非您真的经历了一些严重的困难才能做到这一点。尝试将 index.html 重命名为 index.php。
<?php
$fields = array('name', 'email', 'telephone');
$error = false;
foreach($fields AS $fieldname) {
if(!isset($_POST['fieldname']) || empty($_POST['fieldname'])) {
$errmsg = 'Please Fill All Fields. '.$fieldname.' is empty!<br />';
$error = true;
}
}
if(!$error) {
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$message = $_POST['message'];
$headers = "From: contact@example.com\r\n";
$to = 'me@example.com';
$subject = 'New Lead';
$body = "From: $name\n E-Mail: $email\n Phone: $telephone\n Message: $message";
mail('me@example.com', $subject, $body, $headers);
header("Location: http://www.example.com/messagesent.html");
die();
}
}
?>
**Main Error:**
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
// your code
}
above line should be
if(!isset($_POST['fieldname']) || empty($_POST['fieldname'])) {
}
**Use Ajax Method**
**index.html**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>using Ajax in jQuery</title>
<style type="text/css">
label{
display: block;
margin-bottom: 10px;
}
</style>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("form").submit(function(event){
// Stop form from submitting normally
event.preventDefault();
/* Serialize the submitted form control values to be sent to the web server with the request */
var formValues = $(this).serialize();
// Send the form data using post
$.post("contact.php", formValues, function(data){
// Display the returned data in browser
$("#result").html(data);
});
});
});
</script>
</head>
<body>
<form>
<label>Name: <input type="text" name="name"></label>
<input type="submit" value="Send">
</form>
<div id="result"></div>
</body>
</html>
contact.php
<?php
$fields = array('name');
$error = false;
foreach($fields AS $fieldname) {
if(!isset($_POST['name']) || empty($_POST['name'])) {
$errmsg = 'Please Fill All Fields. '.$fieldname.' is empty!<br />';
$error = true;
}
}
if($error==true){
echo $errmsg;
die();
}
else{
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$headers = "From: contact@example.com\r\n";
$to = 'me@example.com';
$subject = 'New Lead';
$body = "From: $name\n E-Mail: $email\n Phone: $telephone\n Message: $message";
mail('me@example.com', $subject, $body, $headers);
header("Location: http://www.example.com/messagesent.html");
die();
}
}
?>
好的,所以这里的问题是操作属性将您发送到 contact.php,它实际上没有输出任何 html 代码(因此是空白页)。您的 contact.php 文件实际上只是一个用于在表单中执行功能的帮助文件,因此将用户发送到该文件是没有意义的。当然,他们应该留在 index.php 文件中。
所以首先,将表单标记中的 "action" 属性更改为指向 index.php 而不是 contact.php。
其次,您需要要求 contact.php 文件,以便 index.php 文件实际上可以访问您的 php 函数。
第三,因为我们是直接提交到同一个页面,你需要在加载表单之前检查表单是否已经提交。
所以你的代码应该看起来像这样:
<?php
require 'contact.php'; // Path to your contact.php script
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Contact form</title>
</head>
<body>
<!-- Contact Form -->
<form class="form-horizontal" role="form" method="post" action="index.php">
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { // I'm putting my if statement to check if form is submitted here, but you could put it anywhere you like as long as it's wrapping the error message in some way.?>
<div class="form-group">
<span class="error">* <?php echo $errmsg;?></span>
<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" placeholder="First & Last Name">
</div>
</div>
<?php } ?>
<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" placeholder="example@domain.com">
</div>
</div>
<div class="form-group">
<label for="telephone" class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="telephone" name="telephone" placeholder="250-555-555">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
</div>
</form>
<!-- End Contact Form -->
</body>
</html>
我正在构建一个 PHP 表单。该脚本循环遍历表单字段并告诉我它们是否为空。当我提交表单时,它会将我从 index.html 重定向到 contact.php 并显示一个空白页面。我想在 index.html 中的表格上方显示 $errmsg。我究竟做错了什么?这是我从中提取信息的地方:http://www.w3schools.com/php/php_form_required.asp
PHP:
<?php
$fields = array('name', 'email', 'telephone');
$error = false;
foreach($fields AS $fieldname) {
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
$errmsg = 'Please Fill All Fields. '.$fieldname.' is empty!<br />';
$error = true;
}
}
HTML:
<form class="form-horizontal" role="form" method="post" action="contact.php">
<div class="form-group">
<span class="error">* <?php echo $errmsg;?></span>
这是我的全部 contact.php 以防问题进一步恶化。
<?php
$fields = array('name', 'email', 'telephone');
$error = false;
foreach($fields AS $fieldname) {
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
$errmsg = 'Please Fill All Fields. '.$fieldname.' is empty!<br />';
$error = true;
}
}
if(!$error) {
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$message = $_POST['message'];
$headers = "From: contact@example.com\r\n";
$to = 'me@example.com';
$subject = 'New Lead';
$body = "From: $name\n E-Mail: $email\n Phone: $telephone\n Message: $message";
mail('me@example.com', $subject, $body, $headers);
header("Location: http://www.example.com/messagesent.html");
die();
}
}
?>
编辑:整个 HTML 表格。它嵌套在 Bootstrap 对不起意大利面
<!-- Contact Form -->
<form class="form-horizontal" role="form" method="post" action="contact.php">
<div class="form-group">
<span class="error">* <?php echo $errmsg;?></span>
<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" placeholder="First & Last Name">
</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" placeholder="example@domain.com">
</div>
</div>
<div class="form-group">
<label for="telephone" class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="telephone" name="telephone" placeholder="250-555-555">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
</div>
</form>
<!-- End Contact Form -->
您不能 运行 PHP 在 html 文件中编写代码,除非您真的经历了一些严重的困难才能做到这一点。尝试将 index.html 重命名为 index.php。
<?php
$fields = array('name', 'email', 'telephone');
$error = false;
foreach($fields AS $fieldname) {
if(!isset($_POST['fieldname']) || empty($_POST['fieldname'])) {
$errmsg = 'Please Fill All Fields. '.$fieldname.' is empty!<br />';
$error = true;
}
}
if(!$error) {
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$message = $_POST['message'];
$headers = "From: contact@example.com\r\n";
$to = 'me@example.com';
$subject = 'New Lead';
$body = "From: $name\n E-Mail: $email\n Phone: $telephone\n Message: $message";
mail('me@example.com', $subject, $body, $headers);
header("Location: http://www.example.com/messagesent.html");
die();
}
}
?>
**Main Error:**
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
// your code
}
above line should be
if(!isset($_POST['fieldname']) || empty($_POST['fieldname'])) {
}
**Use Ajax Method**
**index.html**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>using Ajax in jQuery</title>
<style type="text/css">
label{
display: block;
margin-bottom: 10px;
}
</style>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("form").submit(function(event){
// Stop form from submitting normally
event.preventDefault();
/* Serialize the submitted form control values to be sent to the web server with the request */
var formValues = $(this).serialize();
// Send the form data using post
$.post("contact.php", formValues, function(data){
// Display the returned data in browser
$("#result").html(data);
});
});
});
</script>
</head>
<body>
<form>
<label>Name: <input type="text" name="name"></label>
<input type="submit" value="Send">
</form>
<div id="result"></div>
</body>
</html>
contact.php
<?php
$fields = array('name');
$error = false;
foreach($fields AS $fieldname) {
if(!isset($_POST['name']) || empty($_POST['name'])) {
$errmsg = 'Please Fill All Fields. '.$fieldname.' is empty!<br />';
$error = true;
}
}
if($error==true){
echo $errmsg;
die();
}
else{
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$headers = "From: contact@example.com\r\n";
$to = 'me@example.com';
$subject = 'New Lead';
$body = "From: $name\n E-Mail: $email\n Phone: $telephone\n Message: $message";
mail('me@example.com', $subject, $body, $headers);
header("Location: http://www.example.com/messagesent.html");
die();
}
}
?>
好的,所以这里的问题是操作属性将您发送到 contact.php,它实际上没有输出任何 html 代码(因此是空白页)。您的 contact.php 文件实际上只是一个用于在表单中执行功能的帮助文件,因此将用户发送到该文件是没有意义的。当然,他们应该留在 index.php 文件中。
所以首先,将表单标记中的 "action" 属性更改为指向 index.php 而不是 contact.php。
其次,您需要要求 contact.php 文件,以便 index.php 文件实际上可以访问您的 php 函数。
第三,因为我们是直接提交到同一个页面,你需要在加载表单之前检查表单是否已经提交。
所以你的代码应该看起来像这样:
<?php
require 'contact.php'; // Path to your contact.php script
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Contact form</title>
</head>
<body>
<!-- Contact Form -->
<form class="form-horizontal" role="form" method="post" action="index.php">
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { // I'm putting my if statement to check if form is submitted here, but you could put it anywhere you like as long as it's wrapping the error message in some way.?>
<div class="form-group">
<span class="error">* <?php echo $errmsg;?></span>
<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" placeholder="First & Last Name">
</div>
</div>
<?php } ?>
<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" placeholder="example@domain.com">
</div>
</div>
<div class="form-group">
<label for="telephone" class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="telephone" name="telephone" placeholder="250-555-555">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
</div>
</form>
<!-- End Contact Form -->
</body>
</html>