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.

share|improve this question
1  
Saving HTML files is probably not what you should be doing. You should generally save only the unique data and use some kind of HTML templating to generate HTML only when it is needed. – roo2 Jan 4 '14 at 5:35
    
Any recommendations for a php based HTML templating engine? – Kahtaf Alam Jan 4 '14 at 5:37
    
since your using angular, I would use angular's built in templating docs.angularjs.org/guide/templates – roo2 Jan 4 '14 at 5:43
    
You might want to use some template generting library like inbuilt Anguar templating or something like Handlebars – sachinjain024 Jan 4 '14 at 6:53

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);
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.