[英]PHP form submit with validation & Bootstrap issue
我真的是PHP和Bootstrap的新手,几乎所有东西。 我一直在尝试遵循有关使用Bootstrap创建表单以及使用PHP通过电子邮件发送表单的一些教程。 我有部分工作了,其他人没有。 我一直想做的就是获取表单,以在未满足参数的情况下在每个表单元素下显示错误消息。 然后,一旦正确填写了所有内容,则无需刷新页面即可提交表单,但确实会显示成功消息。 我一直在合并几本教程,但是这是我尝试使用错误和成功消息进行模仿的教程。 https://bootstrapbay.com/blog/working-bootstrap-contact-form/
当前,如果所有内容均正确填写,但该表单将提交并通过电子邮件发送,但会加载.php文件并坐在空白屏幕上。 如果未输入名称,则它不会发送电子邮件,而只会加载空白屏幕的.php文件。 任何帮助,将不胜感激! 以下是我的PHP和HTML代码段
HTML
<form name="adoptionForm" method="post" action="phpForms/adoption_doc.php" class="form-horizontal" role="form"> <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" placeholder="Your Name"> <?php echo "<p class='text-danger'>$errName</p>";?> </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="Your Email"> <?php echo "<p class='text-danger'>$errEmail</p>";?> </div> <div class="form-group"> <div class="col-lg-offset-2 col-lg-10"> <button type="submit" class="btn btn-default"> Send Message </button> </div> </div> </form>
PHP
<?php /* Set e-mail recipient */ $myemail = "stackoverflowTest@gmail.com"; /* POSTS INFO */ $name = $_POST['name']; $email = $_POST['email']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state']; /* Let's prepare the message for the e-mail */ $subject = "$name has sent you a message"; $message = " Someone has sent you a message using your contact form: Name: $name Email: $email Address: $address City: $city State: $state "; // Check if name has been entered if (!$_POST['name']) { $errName = 'Please enter your name'; } /* Send the message using mail() function */ if (!$errName && !$errEmail) { if (mail ($myemail, $subject, $message)) { $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>'; } } ?>
我测试了此代码,它是您尝试使用的代码的更新副本,应该可以正常工作。
您将需要将表单上的操作更改为页面名称。
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$to = 'stackoverflowTest@gmail.com';
$subject = "$name has sent you a message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
} else {
}
// 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';
} else {
$from = $email;
}
// set body of message to be sent
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Address: $address
City: $city
State: $state
";
//Check if address has been entered
if (!$_POST['address']) {
$errAddress = 'Please enter your street address';
} else {
$address = $_POST['address'];
}
//Check if city has been entered
if (!$_POST['city']) {
$errCity = 'Please enter your city';
} else {
$city = $_POST['city'];
}
//Check if state has been entered
if (!$_POST['state']) {
$errState = 'Please enter your state';
} else {
$address = $_POST['state'];
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errAddress && !$errCity && !$errState) {
if (mail($to, $subject, $message, $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>';
}
} else {
$result = '<div class="alert alert-danger">Error in checking.</div>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Bootstrap contact form with PHP example by BootstrapBay.com.">
<meta name="author" content="BootstrapBay.com">
<title>Adoption Form</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Adoption Form</h1>
<form class="form-horizontal" role="form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<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" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>"; ?>
</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" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>"; ?>
</div>
</div>
<div class="form-group">
<label for="address" class="col-sm-2 control-label">Street Address</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="address" name="address" placeholder="Your Street Address">
<?php echo "<p class='text-danger'>$errAddress</p>"; ?>
</div>
</div>
<div class="form-group">
<label for="city" class="col-sm-2 control-label">City</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="city" name="city" placeholder="Your City">
<?php echo "<p class='text-danger'>$errCity</p>"; ?>
</div>
</div>
<div class="form-group">
<label for="state" class="col-sm-2 control-label">State</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="state" name="state" placeholder="Your State">
<?php echo "<p class='text-danger'>$errState</p>"; ?>
</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 class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.