【问题标题】:How can i echo input data fields in an file_get_contents html template?如何在 file_get_contents html 模板中回显输入数据字段?
【发布时间】:2017-08-28 19:35:46
【问题描述】:

我有这个 send_mail.php 文件。我正在尝试回显一些插入表单的字段,但我不能使用回显功能。我做错了什么?

<?php

$date = $_POST['date'];
$full_name = $_POST['full_name'];
$biz_name = $_POST['biz_name'];
$activity = $_POST['activity'];
$afm = $_POST['afm'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$mobile = $_POST['mobile'];
$address = $_POST['address'];
$city = $_POST['website'];
$email = $_POST['email'];
$discount = $_POST['discount'];
$payment_amount = $_POST['payment_amount'];
$seller_name = $_POST['seller_name'];


$to = $email;
$subject = "Welcome to our site!";


$htmlContent = file_get_contents("email_template.html");

// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers = "Content-type: text/html; charset=UTF-8" . "\r\n";

// Send email
if(mail($to,$subject,$htmlContent,$headers)):
    $successMsg = 'Mail sending successful.';
else:
    $errorMsg = 'Oops! Something went wrong.';
endif;
?>

我的 email_template.html 看起来像这样:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Welcome to our site</title>
<head/>
    <body>
         <h1> Welcome <?php echo $full_name; ?> </h1>
         ...
    </body>
</html>

我是网络开发的新手,所以要温柔! :P

【问题讨论】:

    标签: php html email templates encoding


    【解决方案1】:

    编辑模板并通过符号或特殊名称标记变量,而不是替换真实变量: email_template.html:

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Welcome to our site</title>
    <head/>
        <body>
             <h1> Welcome --full_name-- </h1>
             ...
        </body>
    </html>
    

    php:

    <?php
    $htmlContent = file_get_contents("email_template.html");
    $htmlContent = str_replace("--full_name--",$_POST['full_name'],$htmlContent);
    

    【讨论】:

      【解决方案2】:

      file_get_contents() 不会将文件解析为 php。所以一种选择是做这样的事情:

      ob_start();
      include('email_template.html');
      $htmlContent = ob_get_contents();
      ob_end_clean();
      

      当您使用include() 时,它假定您正在运行php 代码。然后我们使用输出缓冲区来捕获内容。

      这样所有变量仍然可用,否则你需要设置一个完整的模板引擎,在这种情况下我会使用现有的,比如 Twig。

      【讨论】:

        【解决方案3】:

        我刚刚学会了如何像简单的文字一样回答,我的第一个回答完全是“编码”的。

        好吧,我建议你学习codeigniter,它肯定不是最好的,但在我看来它是最简单的,现在最著名的是laravel。

        对于初学者来说,没有什么是容易的,但是在开始研究任何框架之前,首先要了解 php 和 php oo。

        再见! :)

        【讨论】:

          【解决方案4】:

          这两个文件是如何连接的?你在用ajax吗?

          看,如果您使用 ajax,只需返回 $full_name,然后使用 jquery 或 javascript 打印它。 (我相信你没有这样做,但如果你这样做......)

          否则,如果您刚刚发布并重定向到send_mail.php,只需在同一页面上使用echo,就像:

          // Send email
          if (mail($to,$subject,$htmlContent,$headers)):
               echo = 'Mail sending successful.';
          else :
              echo = 'Oops! Something went wrong.';
          endif ;
          

          我可以向您建议的第三种方法是使用$_SESSION
          send_mail.php 开始一个会话:

          session_start();
          //and declare
          $_SESSION["full_name "]= $fullname;
          

          然后,当此用户在您的网站上时,您将为任何页面启用此变量,您只需以这种方式回显

          echo $_SESSION['full_name'];
          

          当你感觉有一点进步时,我建议你学习一个 php 框架,这对你有很大帮助。祝你好运!

          【讨论】:

          • 我只是使用重定向到 send_mail.php 的 html 表单。有什么关于 php 框架的建议吗?
          猜你喜欢
          • 1970-01-01
          • 2013-06-05
          • 2021-02-23
          • 2016-03-30
          • 2020-07-13
          • 2015-01-14
          • 2018-03-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多