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.

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.