0

My goal is to generate an html file in javascript (based on form inputs), and use Angular's $http to send this generated file to the server (php) for storage. Is this possible?

So far I've tried generating HTML strings on the client side and send the string over, and use php's file_put_contents() to generate the html file. But I would like to generate the html file on the client side. As the html gets more complex, I don't want to be sending long and complex strings.

Javascript (inside the controller)

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

test.php

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

I prefer to generate the html file on the client side because I want to reduce server load.

Thanks for any advice.

Kahtaf Alam
  • 463
  • 2
  • 7
  • 19

1 Answers1

0

You can use Twig for PHP templating. Or PHP Plates - http://platesphp.com/

Read the POSTed data from PHP and generate using the template.

Pseudo codes:

<?php
    $input = $_POST['content'];
    $html = generateTemplate($input);
    file_put_contents($fileName, $html);
?>
masnun
  • 11,635
  • 4
  • 39
  • 50