我正在构建一个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.comrn";
$to = 'me@example.com';
$subject = 'New Lead';
$body = "From: $namen E-Mail: $emailn Phone: $telephonen Message: $message";
mail('me@example.com', $subject, $body, $headers);
header("Location: http://www.example.com/messagesent.html");
die();
}
}
?>
编辑:整个HTML表单。它嵌套在Bootstrap中对不起,是spaghetti
<!-- 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 -->
**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.comrn";
$to = 'me@example.com';
$subject = 'New Lead';
$body = "From: $namen E-Mail: $emailn Phone: $telephonen Message: $message";
mail('me@example.com', $subject, $body, $headers);
header("Location: http://www.example.com/messagesent.html");
die();
}
}
?>
您不能在html文件中运行PHP代码,除非您确实经历了一些严重的困难。尝试将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.comrn";
$to = 'me@example.com';
$subject = 'New Lead';
$body = "From: $namen E-Mail: $emailn Phone: $telephonen 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'])) {
}
好的,这里的问题是action属性将您发送到contact.php,它实际上没有输出任何html代码(因此是空白页)。php文件实际上只是一个在表单中执行函数的辅助文件,因此将用户发送到该文件是没有意义的。当然,它们应该留在index.php文件中。
所以首先,改变表单标签中的"action"属性,使其指向index.php而不是contact.php。
第二,您需要需要您的contact.php文件,以便您的php函数实际上可以被index.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>