[英]Bootstrap Contact Form Processing
編寫一個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>
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>
<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>
郵寄您需要在這里閱讀
使用GMAIL郵件服務器從在PHP中運行XAMMP的本地主機發送電子郵件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.