Jump to content

Javascript creating/writing to a text file from HTML form


louisstephens

Recommended Posts

Use the onclick event and submit the form, but on the server side do not write a page. Instead do the following in PHP:

 

header("Content-disposition: attachment; filename="whatever.txt");

header("Content-type: document/text");

 

- echo your lines ending each with "\r\n";

 

return;

 

This will cause a view/save dialog to appear at the client. Be sure and use return to kill the script so no additional output can occur such as an extra linefeed after the PHP close tag.

 

Yes, but you can use one PHP file for all of it. When the script begins executing, check $_POST["button"] to see if it has a value. If yes, that means the page has already been rendered and someone clicked a button on the page. You can also use $_POST["usertext"] to obtain the text the user entered. You would then just stream a text file back out to the client and not output an HTML page. Otherwise, output the HTML page from your PHP script in the normal fashion, and when you output the page, be sure and include the form element, button, and text box. The action or URL of the form element should be the same PHP script.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.