434,896 Members | 1,983 Online
Need help? Post your question and get tips & solutions from a community of 434,896 IT Pros & Developers. It's quick & easy.

# Save exact contents of web form (including special characters) to file?

 P: n/a Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box and save it to a file. This step is not to hard however the contents of the textarea is mostly latex source so it contains just about every special character you can imagine. My question is this, how do I save an exact copy of the textarea contents with special characters, carriage returns, etc to a file? Any help would be great, Greetings, David. Mar 6 '06 #1
8 Replies

 P: n/a da*****************@gmail.com wrote: Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box and save it to a file. This step is not to hard however the contents of the textarea is mostly latex source so it contains just about every special character you can imagine. My question is this, how do I save an exact copy of the textarea contents with special characters, carriage returns, etc to a file? Any help would be great, Greetings, David. I am an amateur at this, but your problem could be better defined. I.e. is your problem:- (a) you don't know at all how to extract the contents of a TEXTAREA element using the BOM/DOM (browser object model/document object model) and save this using the ActiveX Scripting.FileSystemObject (IE only) or XPCOM (Firefox)? (b) when you extract the contents of a TEXTAREA using the BOM, the Javascript String does not contain all of the "special characters" you expect it to. In which case, can you be explicit as to which characters are causing a problem for you. A Javascript String consists of 16bit characters, and so can represent unicode values from 0000 to FFFF I think. (c) when you save the contents to a text file, the component throws an error or the resulting save file does not contain all of the "special characters" you expect it to. In which case, again can you be explicit as to which characters are causing a problem for you. If your problem is (c), and you are working in Windows, then you may need to become familiar with "code pages" (I only have a rough knowledge of them). For english code pages, when Scripting.FileSystemObject saves a text file, there are some characters (I think in the ascii range 129 to 159) which can cause errors. Regards Julian Turner Mar 6 '06 #2

 P: n/a da*****************@gmail.com wrote: [...] I am trying to take the contents of a textarea box and save it to a file. This step is not to hard however the contents of the textarea is mostly latex source so it contains just about every special character you can imagine. (La)TeX source is plain text, I do not see why that would be relevant. Are confusing the source code with the output it generates? My question is this, how do I save an exact copy of the textarea contents with special characters, carriage returns, etc to a file? The same way you save other content to a file, which has been discussed here several times before. You are going to retrieve the value with a recent ECMAScript implementation (because older implementations do not correspond with the required host object models). All of those use UTF-16 as character string encoding, it does not matter what characters are in there. PointedEars Mar 6 '06 #3

 P: n/a Ok, thanks for the replies although a lot of it was over my head. I have been learning web scripting for 3 or 4 days now so be please dumb it down a bit. I think my problem is (b) when you extract the contents of a TEXTAREA using the BOM, theJavascript String does not contain all of the "special characters" youexpect it to. In which case, can you be explicit as to whichcharacters are causing a problem for you. A Javascript String consistsof 16bit characters, and so can represent unicode values from 0000 toFFFF I think. and it is the "+" symbol that I seem to be missing, at least thats the only one missing from my short test latex file. (La)TeX source is plain text, I do not see why that would be relevant.Are confusing the source code with the output it generates? When I say special characters I mean things like "+/!$&<" not things like alpha, beta, gamma etc you see in LaTex output. I believe my problem may lie in either the way I am retrieving the contents of the textarea or in the way i am saving it to a file. I have included a striped down version of my code that still does not save the "+" symbol but does save the carriage returns after including the escape() function and stripslashes function in the php script used to write the file. sample.html save.php Mar 6 '06 #4  P: n/a da*****************@gmail.com wrote: Ok, thanks for the replies although a lot of it was over my head. I have been learning web scripting for 3 or 4 days now so be please dumb it down a bit. I think my problem is(b) when you extract the contents of a TEXTAREA using the BOM, theJavascript String does not contain all of the "special characters" youexpect it to. In which case, can you be explicit as to whichcharacters are causing a problem for you. A Javascript String consistsof 16bit characters, and so can represent unicode values from 0000 toFFFF I think. and it is the "+" symbol that I seem to be missing, at least thats the only one missing from my short test latex file.(La)TeX source is plain text, I do not see why that would be relevant.Are confusing the source code with the output it generates? When I say special characters I mean things like "+/!$&<" not things like alpha, beta, gamma etc you see in LaTex output. I believe my problem may lie in either the way I am retrieving the contents of the textarea or in the way i am saving it to a file. I have included a striped down version of my code that still does not save the "+" symbol but does save the carriage returns after including the escape() function and stripslashes function in the php script used to write the file. sample.html

 P: n/a First of all, please learn to post. NetNews is thread-based; you should post a followup to the text you are directly referring to, provide attribution of quoted material (in Google Groups: show options, Reply), and trim your quotes to the minimum necessary to retain context: That said, for the flaw in its Web interface, posting with Google Groups is recommended against anyway. Use a decent newsreader application instead. news:news.software.readers __________________________________________________ _________________________ da*****************@gmail.com wrote: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ You better use your name here. Ok, thanks for the replies although a lot of it was over my head. I have been learning web scripting for 3 or 4 days now so be please dumb it down a bit. I will try. However, never hesitate to ask explicitly about things you do not (fully) understand. I think my problem is [Julian Turner wrote:] (b) when you extract the contents of a TEXTAREA using the BOM, the Javascript String does not contain all of the "special characters" you expect it to. In which case, can you be explicit as to which characters are causing a problem for you. A Javascript String consists of 16bit characters, and so can represent unicode values from 0000 to FFFF I think. and it is the "+" symbol that I seem to be missing, at least thats the only one missing from my short test latex file. Because you are including it in the request URI, even though you are using POST. The plain +' character is considered a substitute for the space character in the query component by many server-side applications, including PHP. [Thomas 'PointedEars' Lahn wrote:] (La)TeX source is plain text, I do not see why that would be relevant. Are confusing the source code with the output it generates? When I say special characters I mean things like "+/!$&<" not things like alpha, beta, gamma etc you see in LaTex output. Of those characters, only +' and &' can be considered special, and only within the query component of a URI. (See above for the +' character.) The &' character is the delimiter between each parameter of the component, so it must be escaped if it does not serve as such (see RFC3986; you do this already). I believe my problem may lie in either the way I am retrieving the contents of the textarea or in the way i am saving it to a file. Your assumption is correct. The DOCTYPE declaration is missing before this, so the markup is not Valid. Consider not using tables here, and learn to use CSS. You are looking for instead of the deprecated center' element. Whenever you use event handler attributes such as onclick', you should declare the default scripting language in the 'head' element: ... ... Do not use the br' element to achieve margins. Use the CSS padding' and margin' properties instead. See above. You are confusing programming and markup language here. Commas are not allowed within a (start) tag outside of CDATA attribute values. They may be ignored, but one should not depend on that. Remove them. Attribute values should always be single-quoted or double-quoted. Although not required, maybe it is better for later element object access to replace the space in the name with an underscore (_) or, if you omit the ID attribute as rendered redundant by my suggestion above, just remove the space. You should have a preference for lowercase names and IDs. See above. save.php Do not use stripslashes() here. Use$_POST instead of $_GET here (see above).$filename=implode($array1); Unnecessary inefficient. Consider$filename = date('YmdHms') . '.tex'; instead. $f=fopen($filename,"w"); fwrite($f,$stuff); fclose($f); ?> You should test whether fopen() was successful before you fwrite() and fclose():$f = fopen($filename, "w"); if ($f) { fwrite($f,$stuff); fclose($f); } HTH PointedEars Mar 7 '06 #6  P: n/a da*****************@gmail.com wrote: When I say special characters I mean things like "+/!$&<" not things like alpha, beta, gamma etc you see in LaTex output. I believe my problem may lie in either the way I am retrieving the contents of the textarea or in the way i am saving it to a file. I have included a striped down version of my code that still does not save the "+" symbol but does save the carriage returns after including the escape() function and stripslashes function in the php script used to write the file. That can be two issues at once: 1) The infamous "Korean issue" in IE if you server doesn't set charset. * Always declare charset on your page - or be sure that your server will provide one * 2) "+" is ised in CGI to transmit encoded chars. * Always encode your values before sending them * (on regular GET / POST browser does it for you, with ajaxoids you have to do it manually) Mar 7 '06 #7

 P: n/a Thomas 'PointedEars' Lahn wrote: First of all, please learn to post. NetNews is thread-based; you should post a followup to the text you are directly referring to, provide attribution of quoted material (in Google Groups: show options, Reply), and trim your quotes to the minimum necessary to retain context: That said, for the flaw in its Web interface, posting with Google Groups is recommended against anyway. Use a decent newsreader application instead. Still working on the decent newsreader but hopefully I am posting correctly this time. da*****************@gmail.com wrote: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ You better use your name here. This should be fixed now $stuff=stripslashes($_GET['content']); This line removes all backslashes (e.g. from "\tilde{}"): Do not use stripslashes() here. I have implemented all the recommendations succesfully except this one. I found that even when using the _POST I still needed to include the stripslashes() function or the saved file included extra slashes. For example, without the stripslashes() the saved file would include " \' " instead of " ' " and "\\end{document}" instead of "\end{document}" So using the following code gives me the results I want, hopefully it is a little more correct now. Please point out any amatuer mistakes sample.html Test Latex Save save.php sample.css div#main {background: white; padding: 10px; text-align: center;} Mar 8 '06 #8  P: n/a David L Green wrote: Thomas 'PointedEars' Lahn wrote: Still working on the decent newsreader but hopefully I am posting correctly this time. Looks OK now :) da*****************@gmail.com wrote: [...] >stuff=stripslashes($_GET['content']); This line removes all backslashes (e.g. from "\tilde{}"): Do not use stripslashes() here. I have implemented all the recommendations succesfully except this one. I found that even when using the$_POST I still needed to include the stripslashes() function or the saved file included extra slashes. For example, without the stripslashes() the saved file would include " \' " instead of " ' " and "\\end{document}" instead of "\end{document}" The reason is you have the magic_quotes_gpc' option in php.ini set to on' (default). So using the following code gives me the results I want, hopefully it is a little more correct now. Please point out any amatuer mistakes sample.html Test Latex Save A little bit more indentation would be nice. Usually two spaces suffice. And it is LaTeX' (or LaTeΧ'; or -- almost the original -- LATEΧ', but not within the title' element), really. Compare :) [...] function loadtex(url,form1){ The second argument is named like your form. Both names are unnecessary (the named argument itself is not), see below. var xmlHttp = new XMLHttpRequest(); See my previous followup about this. [...] function savetex(form1){ var updtSrc = encodeURIComponent(form1.elements['texsource'].value); var urlS = 'save.php'; These two variables are not really needed, you can include their values directly as arguments in the method calls. var svHttp = new XMLHttpRequest(); See above. svHttp.open('POST',urlS,true); svHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); See my previous followup about this. Workaround: function isMethodType(s) { return (s == "function" || s == "object"); } if (isMethodType(typeof svHttp.setRequestHeader) && svHttp.setRequestHeader) { svHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded'); } svHttp.send("content=" + updtSrc); } [...]
The div' element appears to be redundant with the CSS declarations below; you can target the body' element directly: body { /* ... */ }
^^^^^ You are still using proprietary references here, referring to the HTMLFormElement by its name. You should not, and you do not have to. As long as the input' element is descendant of a form' element, it knows its ancestor: ...... (BTW, you cannot be sure that the _TeX_ source uses the LaTeX macro package, can you? Therefore, AIUI, it would be better to refer only to TeX explicitly.)
^^^^^ See above. Maybe you also want to write those buttons dynamically, because they will not work without client-side script support. E.g. Without script support, no button is displayed then, which is what we want. (No button, no cry ;-)) In order to maintain your previous column-based layout, you can make the div' elements float around each other with the float' and `clear' CSS properties: However, I just wanted you to take into consideration that tables may not be appropriate here. If you are convinced that your form can be interpreted as tabular data, there is nothing wrong with using tables; in fact, it would be wrong not to use a table then. For most standard Web forms, a table is just fine (for me) as it shows a label-control relationship. [...] save.php $f=fopen($filename,"w"); When string expansion is not needed, use single quotes instead of double quotes in PHP. They are considerably faster: [...] sample.css div#main {background: white; padding: 10px; text-align: center;} ^^^^^^^^^^^^^^^^^^ background-color: white; suffices. And be sure to declare both the background (color) and the foreground color always: The snipped parts looks just fine :) Regards, PointedEars Mar 8 '06 #9

### This discussion thread is closed

Replies have been disabled for this discussion.