如何在提交联系表单时显示用户发件人姓名

问题描述 投票:-3回答:1

我想在用户提交联系表单时在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>";
        }       
    }       
?>

mailer.php

php twitter-bootstrap forms form-submit
1个回答
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']);         
   }
  ?>
© www.soinside.com 2019 - 2024. All rights reserved.