I want to know if it is possible to use a basic html / javascript form to input data and then when the submit button is clicked the result is the creation of a new html document on the web server. The initial form contents would be used to determine what data is added to the new html page when it is created. Presumably using PHP/MySQL. Thanks.
-
Do you want to create a permanent, static page, or just return a document on the fly based on the form data?Barmar– Barmar2012-10-02 01:13:31 +00:00Commented Oct 2, 2012 at 1:13
-
I want to create a permanent document on the server that can be served up at any time.This would be done repeatedly and I would probably incorporate a naming convention script so it each one would be titled something different IE: doc1.html , doc2.html etc. At the bare minimum I want to know if I can have a submit button that "makes" a new "physical" html document on the server each time it's clicked. I'm sure this can be done but searching on google is a pain since the key words flood the search with "how to create a form" results. :(William– William2012-10-02 01:15:46 +00:00Commented Oct 2, 2012 at 1:15
3 Answers
JavaScript runs on the client, not the server, so it can't create files directly. This would be done using a PHP script on the server that processes the form. It's very similar to displaying a page based on the form data, but instead it writes to a file:
$f = fopen($filename, 'w') or die "Can't create file $filename";
fprintf($f, "<html>\n<body>\n");
// and so on
fprintf($f, "</body>\n</html>\n");
fclose($f);
4 Comments
PUT operation, you could construct the entire page in the browser and upload it to the server via an AJAX call with PUT instead of POST. But many servers don't allow this.You can do this but I would strongly recommend against putting something like this on a site. A tool like this would easily allow an attacker to place any type of code on your site they pleased. If you create a tool like this you could password protect the directory using the webserver or create some kind of authentication system. If you want to be able to edit pages using a database there are a lot of content management systems out there to already allow you to do this. Drupal or Wordpress are a couple of good places to start.
Comments
There's no actual need to create the entire html page on the server. A much better way would be to actually save the form's data on the database. When you need to display the data, just populate a template and serve. This'll make managing all those pages much much easier because any change only needs to be done the template, and not to the created files.
For example, consider that your form has a first name, last name and comment field. You would probably store.
{ firstName: 'John', lastName: 'Doe', comment: 'This was good' }
{ firstName: 'Jane', lastName: 'Doe', comment: 'This was bad' }
And you'd probably have a template.html on the server, like so:
<html>
<head><title>View Comment</title></head>
<body>
Name: {{lastName}}, {{firstName}}
Comment: {{comment}}
</body>
</html>
This uses Mustache.js specific syntax but you may choose whatever you like. When a request is made, you just populate the placeholders with the data and voila! a dynamic html page.