0

我的目标是在 javascript 中生成一个 html 文件(基于表单输入),并使用 Angular 的 $http 将此生成的文件发送到服务器 (php) 进行存储。这可能吗?

到目前为止,我已经尝试在客户端生成 HTML 字符串并将字符串发送过来,并使用 php 的 file_put_contents() 生成 html 文件。但我想在客户端生成 html 文件。随着 html 变得越来越复杂,我不想发送长而复杂的字符串。

Javascript(在控制器内部)

$http({
    url: "/test.php",
    method: "POST",
    data: {"content":"<h1>hello world</h1>"}
});

测试.php

<?php
    $input = file_get_contents("php://input");
    $data = json_decode($input);
    file_put_contents("index.html", $data->content);
?>

我更喜欢在客户端生成 html 文件,因为我想减少服务器负载。

感谢您的任何建议。

4

1 回答 1

0

您可以将 Twig 用于 PHP 模板。或 PHP 板 - http://platesphp.com/

从 PHP 读取 POSTed 数据并使用模板生成。

伪代码:

<?php
    $input = $_POST['content'];
    $html = generateTemplate($input);
    file_put_contents($fileName, $html);
?>
于 2014-01-04T10:20:57.007 回答