【问题标题】:php include inside a variable to be used laterphp 包含在一个变量中以供以后使用
【发布时间】:2014-06-18 17:37:30
【问题描述】:

我试图在提交表单时包含整个页面,然后由 PHPMailer 发送。它看起来像这样:

Form1.php:

if (!empty($_POST['email']) {
$variable_email = $_POST['email'];
$body = include (content.php);
Functions::sendEmail($body, $variable_email)

}

<html>
<form action='Form1.php' method=post>
<input name="email"></input>
<button type='submit' />
</form>
</html>

内容.php:

$id = $variable_email

// DB Queries to SELECT stuff based on $id
$db_output = 'information'

<html>
<div><?php echo $db_output; ?></div>
</html>

当我提交表单时,它会立即在页面中包含 content.php,并且不会在电子邮件功能中发送内容。

我已经尝试按照其他地方的建议走 ob_start() 路线,但是当我输出 get_file_contents() 时,它会输出所有内容,包括 php 和 sql 查询。需要明确的是,我要发送的内容/电子邮件应该只包含 html 部分。

我已经从多个角度看待这个问题,但我似乎无法让它发挥作用。我应该以另一种方式接近它吗?理想情况下,我只想将我的整个 contents.php 文件输出到函数的变量中,并让它看起来与我在 HTML 页面上包含相同的文件时完全一样,即有没有办法在不执行的情况下“准备”包含语句马上?

谢谢!

【问题讨论】:

  • 当使用 ob_start 时,您需要 ob_get_contents(并且可能需要 ob_get_clean)。 file_get_contents 用于以文本形式打开和读取文件,以获取文件的来源。
  • file_get_contents() 只是吞食字节并将它们塞进一个字符串中。它不会执行在该字节流中找到的任何内容。这就是为什么有include。 include (模糊地)相当于eval(file_get_contents(...))
  • 感谢@JonathanKuhn - 如果您将其添加为答案,我会接受。设法让这个工作在你的提示下进行了一些摆弄。我实现了:ob_start();包括('./content.php'); $body = ob_get_clean();然后传递给我的函数。干杯!
  • 添加了答案。如果我昨天不那么忙,我会发布一个例子。很高兴你得到它。

标签: php function include


【解决方案1】:

当使用 ob_start 时,您需要 ob_get_contents(可能还需要 ob_get_clean)。 file_get_contents 用于以文本形式打开和读取文件,以获取文件的来源。

在Form1.php中:

if (!empty($_POST['email'])) {
    $variable_email = $_POST['email'];

    ob_start();
    include 'content.php';
    $body = ob_get_clean();

    Functions::sendEmail($body, $variable_email);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-10
    • 2013-08-27
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    相关资源
    最近更新 更多