编写一个Bootstrap联系表单,并在异常时使用。提交表单时,它不会告诉用户邮件已发送。我希望当用户使用成功的消息将其获取的数据发送到同一页面上时。谁能告诉我我做错了什么或给我一个解决方法?
<form class="form-horizontal" role="form" method="post" action="">
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = '3Form';
$to = 'info@gmail.com';
$subject = 'Message from 360 Form ';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\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';
}
//Check Phone
if (!$_POST['phone']) {
$errPhone = 'Please enter a phone Number';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// 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! we 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>';
}
}
}
?>
<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="2 + 3 = ?">
</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">
<! Will be used to display an alert to the user>
</div>
</div>
</form>
答案 0 :(得分:0)
i made a few changes in your code you have to use the isset function in php, and for the error message you should create it in your CSS file, last thing i removed the authentication because i prefer to write a js/jquery .
<body>
<?php if (!isset($_POST['name'])) { ?>
<form class="form-horizontal" role="form" method="post" action="">
<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="2 + 3 = ?">
</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>
<?php }
else {
//connect to the db
include 'dbconnect.php';
//collect the name data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = '360 Web Fform';
$to = 'info@360websolution.com';
$subject = 'Message from 360 Form ';
//inserting data to the DB
$sql = "INSERT INTO YourTable SET
YourTable fields....
mysqli_query($conn,$sql);
echo "<span class="error"> Your request will be processed shortly</span>";
} ?>
</body>
答案 1 :(得分:0)
<form class="form-horizontal" role="form" method="post" action="">
<?php
$result ='';
$errName='';
$errEmail='';
$errPhone='';
$errMessage='';
$errHuman='';
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = '3Form';
$to = 'info@gmail.com';
$subject = 'Message from 360 Form ';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
// Check if name has been entered
if (empty($_POST['name'])) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (empty($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check Phone
if (empty($_POST['phone'])) {
$errPhone = 'Please enter a phone Number';
}
//Check if message has been entered
if (empty($_POST['message'])) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// 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! we 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>';
}
}
}
?>
<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="tel" class="form-control" id="phone" name="phone" placeholder="Phone" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="2 + 3 = ?">
</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">
<?=$result;?>
</div>
</div>
</form>
邮寄你需要在这里阅读
Send email from localhost running XAMMP in PHP using GMAIL mail server