我想在用户提交联系表单时在div alert-success中显示用户名。我怎么能这样做,你能帮我吗? 我有index.php页面和mailer.php =====的index.php =====
<form method="post" action="mailer.php" class="contact-form">
<?php
if (isset($_GET['success'])) {
if ($_GET['success'] == 1) {
echo "<div class=\"alert-success success\">Thank you! Your
message has been sent.</div>";
}else{
echo "<div class=\"alert-danger error\">Sorry! Something went
wrong, please try again.</div>";
}
}
?>
答案 0 :(得分:0)
使用session
在动作脚本的顶部,调用
<?php
/* mailler.php */
session_start();
//Get the form fields, remove html tags and whitespace
$name = strip_tags(trim($_POST['name']));
//... code after
你的行动代码
/* mailler.php */
// ....code before
// Send email
mail($recipient, $subject, $email_content, $email_headers);
//add this code
$_SESSION['success'] = ['state'=> 1, 'name'=> $name];
//Redirect to the index.html page with form fragment
header('Location: http://www.example.com/index.php#form');
exit();
你的表格就像这样
<form method="post" action="mailer.php" class="contact-form">
<?php
if (isset($_SESSION['success'])) {
if ($_SESSION['success']['state'] == 1) {
echo "<div class=\"alert-success success\">Thank you ". $_SESSION['success']['name'] ."! Your
message has been sent.</div>";
}else{
echo "<div class=\"alert-danger error\">Sorry! Something went
wrong, please try again.</div>";
}
unset($_SESSION['success']);
}
?>