所以我有一个来自那个基本上它提交它运行我在“contact-process.php”上的内容,当点击提交时它会转到那个页面,然后显示成功或错误,我该怎么做提交在同一页面上显示成功或错误消息,并清除表单以显示已发送。这是我在网页上的代码。
<form class="form-horizontal" method="post" action="contact-process.php">
<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">
</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@gmail.com">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject2" name="subject2" placeholder="Logo Design - Request">
</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" placeholder="Please explain your request in detail. Provide screenshots & links."></textarea>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">5 x 2 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input type="submit" class="btn-primary btn" id="submit" name="submit" value="Send">
</div>
</div>
</form>
然后这是我在“contact-process.php”上的代码
<?php
if(isset($_POST['submit'])){
$name = $_POST["name"];
$email = $_POST["email"];
$subject2 = $_POST["subject2"];
$message = $_POST["message"];
$human = intval($_POST['human']);
$EmailTo = "example@outlook.com";
$Subject = "Message Received";
if ($human !== 10) {
$errHuman = 'Your anti-spam is incorrect';
}
// prepare email body text
$Body = "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n\n\n";
$Body .= "Subject: ";
$Body .= $subject2;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success){
echo "<div class='alert alert-success'>Thank You! I will be in touch</div>";
}else{
echo "<div class='alert alert-danger'>Sorry there was an error sending your message. Please try again later.</div>";
}
}
?>
答案 0 :(得分:0)
将html放入PHP脚本中,如下所示:
<form class="form-horizontal" method="post" action="#">
<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">
</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@gmail.com">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject2" name="subject2" placeholder="Logo Design - Request">
</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" placeholder="Please explain your request in detail. Provide screenshots & links."></textarea>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">5 x 2 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input type="submit" class="btn-primary btn" id="submit" name="submit" value="Send">
</div>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if($_POST["name"] != "" AND $_POST["email"] != "" AND $_POST["subject2"] != "" AND $_POST["message"] != "") {
$name = $_POST["name"];
$email = $_POST["email"];
$subject2 = $_POST["subject2"];
$message = $_POST["message"];
$human = intval($_POST['human']);
$EmailTo = "example@outlook.com";
$Subject = "Message Received";
if ($human !== 10) {
echo "<div class='alert alert-danger'>Your anti-spam is incorrect</div>";
}else{
// prepare email body text
$Body = "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n\n\n";
$Body .= "Subject: ";
$Body .= $subject2;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success){
echo "<div class='alert alert-success'>Thank You! I will be in touch</div>";
}else{
echo "<div class='alert alert-danger'>Sorry there was an error sending your message. Please try again later.</div>";
}
}
}else{
echo "<div class='alert alert-success'>Data incomplate</div>";
}
}else{
// do nothing
}
?>
修改强>
行动将是:
action="<?php echo $_SERVER['PHP_SELF']; ?>"
OR
action="#"
OR
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"
答案 1 :(得分:0)
先找你......然后问
您需要知道一点Ajax是一种允许您与php javascript / JSON进行通信的技术
示例:
https://scotch.io/tutorials/submitting-ajax-forms-with-jquery
答案 2 :(得分:0)
制作一个带警报功能的脚本,然后在脚本中将location.reload放入成功部分。