我正在构建一个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 -->
答案 0 :(得分:0)
你不能在html文件中运行PHP代码,除非你真的经历了一些严肃的箍。尝试将index.html重命名为index.php。
答案 1 :(得分:0)
<?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'])) {
}
答案 2 :(得分:0)
**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();
}
}
?>
答案 3 :(得分:0)
好的,所以这里的问题是action-attribute将你发送到contact.php,它实际上没有输出任何html代码(因此是空白页面)。您的contact.php文件实际上只是一个帮助文件,用于在表单中执行功能,因此将用户发送到该文件是没有意义的。当然,它们应该保留在index.php文件中。
首先,将form标签中的“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>