我正在尝试在有人填写表单后显示“消息发送成功”。
以下是我的代码
<form action="contact.php" id="footer-form" method="post" role="form">
<div class="form-group has-feedback"><label class="sr-only" for="name2">Name</label> <input class="form-control" id="name2" name="name2" placeholder="Name" required="" type="text" /></div>
<div class="form-group has-feedback"><label class="sr-only" for="email2">Email address</label> <input class="form-control" id="email2" name="email2" placeholder="Enter email" required="" type="email" /></div>
<input class="btn btn-default" type="submit" value="Send" /></form>
<?php 
$name = $_POST['name2'];
$email = $_POST['email2'];
$formcontent="From: $name \nEmail: $email";
$recipient = "email@domain.com";
$subject = "Website Under Construction";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>发布于 2021-03-10 01:06:10
只需将PHP代码置于HTML代码之上并定义一条消息即可。
然后,在您的HTML代码中回显它:
<?php
if(isset($_POST) {
   $name = $_POST['name2'];
   $email = $_POST['email2'];
   $formcontent="From: $name \nEmail: $email";
   $recipient = "email@domain.com";
   $subject = "Website Under Construction";
   $mailheader = "From: $email \r\n";
   mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
   // Set the message here:
   $message = "Message sent!";
}
?>
<form action="contact.php" id="footer-form" method="post" role="form">
<div class="form-group has-feedback"><label class="sr-only" for="name2">Name</label> <input class="form-control" id="name2" name="name2" placeholder="Name" required="" type="text" /></div>
<div class="form-group has-feedback"><label class="sr-only" for="email2">Email address</label> <input class="form-control" id="email2" name="email2" placeholder="Enter email" required="" type="email" /></div>
<input class="btn btn-default" type="submit" value="Send" /></form>
<?php
// Put this wherever you want
if(isset($message)) echo $message;
?>解释
默认情况下,变量$message是未设置的。表单提交完成后,将字符串"Message sent!“赋给变量$message。
现在,在代码中的任何位置,都可以检查是否定义了变量$message并将其打印出来。
发布于 2021-03-10 01:09:49
index.php文件
<form action="contact.php" id="footer-form" method="post" role="form">
 <div class="form-group has-feedback"><label class="sr-only"for="name2">Name</label> 
<input class="form-control" id="name2" name="name2" placeholder="Name" required="" type="text" /></div>
<div class="form-group has-feedback"><label class="sr-only" for="email2">Email address</label>  <input class="form-control" id="email2" name="email2" placeholder="Enter email" required="" type="email" /></div>
 <input class="btn btn-default" type="submit" name="send_mail" value="Send" /> 
</form>contact.php文件
if (isset($_POST['send_mail'])) {
    $name = $_POST['name2'];
    $email = $_POST['email2'];
    $formcontent="From: $name \nEmail: $email";
    $recipient = "email@domain.com";
    $subject = "Website Under Construction";
    $mailheader = "From: $email \r\n";
    $send_email = mail($recipient, $subject, $formcontent, $mailheader);
    if ($send_email) {
       echo 'Your success message';
    } else {
       die("Error!");
    }
 }说明
首先看一下index.php文件,这里的<form action="contact.php" id="footer-form" method="post" role="form">在表单标签上使用了action attribute,并使用了类似contact.php的action路径
还要检查<input class="btn btn-default" type="submit" name="send_mail" value="Send" />,这里我使用了name attribute name="send_mail"
当用户点击该按钮时,该页面将重定向到contact.php页面,并且该页面将检查用户是否点击该按钮并处理mail函数。经过处理后,最后显示消息。
https://stackoverflow.com/questions/66551098
复制相似问题