0

I'm trying to get a code in which users enter in their username and it creates a html file for them. I'm having a hard time getting the code to work and would appreciate some help. Also, if I were to upload this code, would it work or do I have to do something serversided?

<.html>
<.head>
<.title><./title>
<./head>
<.body>
<.script>
function WriteToFile(var userName) {
    //set fso = CreateObject("Scripting.FileSystemObject");  
    //var userName = document.getElementById('user_name').value;
    File file = new File(userName + ".html");
    //set newFile = fso.CreateTextFile(userName + ".html");
    //BufferedWriter output = new BufferedWriter(new FileWriter(file));
    //output.write("test");
    //output.close();
}

<./script>
<.form action="">
<.p>
User name: <.input type="text" id="user_name">
<.input type="submit" value="Create new page" onclick="return WriteToFile(user_name);">
<./p>
<./form>
<./body>
<./html>
Jay
  • 73
  • 3
  • 14
  • 1
    Javascript is not java – mguimard Apr 25 '14 at 14:41
  • Someone correct me if I am wrong, but Javascript can't write to files. You can use AJAX to call a PHP script which creates temporary file on the server and then download it on post-back. But that's a lot more involved. – Kivak Wolf Apr 25 '14 at 14:42
  • You are writing Java. You need to write Javascript. – Tim Apr 25 '14 at 14:43
  • @Kivak Wolf You are right. Javascript cannot write files. Do you really want to write in a file ? Don't you just want to load the generated html in the page ? If you really want to write in a file, then Kivak solution would work i guess – Larta Apr 25 '14 at 14:47
  • Yes I'm trying to have user enter their name and I want the server to automatically create a page for them haha. Can this not be done with javascript? – Jay Apr 25 '14 at 15:01
  • @Jay Not with pure javascript. You need to create a PHP page which creates a file (http://www.w3schools.com/php/php_file.asp) and then use an AJAX GET request (probably using jquery https://api.jquery.com/jQuery.get/) to send the data to the PHP page (name? other data?) and then get the name and location of the newly created file. – Kivak Wolf Apr 25 '14 at 15:14
  • If you want this solution, I can answer the question with some code for you. :) – Kivak Wolf Apr 25 '14 at 15:16
  • 1
    Actually, you can do this with javascript only, no need to have an external server. There are many way to achieve this, some ways could be found here http://stackoverflow.com/questions/13405129/javascript-create-and-save-file – mguimard Apr 25 '14 at 15:23

1 Answers1

0

It would probably be easier to set up a small database which gets populated with the user's information, then create a template website which displays data from the user's table in the database.

KiaShakiba
  • 128
  • 9
  • Do you know if that could be done with javascript or would I need something like PHP – Jay Apr 25 '14 at 16:18
  • You would need to use PHP, but it's very simple! – KiaShakiba Apr 25 '14 at 18:52
  • Or, if you want to stay away from database programming, you could use PHP to output the new file ([see here](http://www.php.net/manual/en/function.file-put-contents.php)) with this code at the top of your page: – KiaShakiba Apr 25 '14 at 19:02
  • Sorry, I forgot the isset() around the post statement, in the if. – KiaShakiba Apr 25 '14 at 19:09