Note that there are some explanatory texts on larger screens.

plurals
  1. POWrite into a text file with jQuery
    primarykey
    data
    text
    <p>I am trying to read and write into text files using jQuery. I have already written the function to read a file; but I can't do is write into a file. I have 2 files, <code>read.txt</code> and <code>write.txt</code>, in the same folder as the code.</p> <p>The two jQuery functions (below, with surrounding server-side Perl code) are:</p> <pre><code>&lt;!-- language: perl --&gt; my $script = qq{ \$(document).ready(function() { \$("#readFile").click(function() { \$.get('read.txt', function(data) { \$("#container").val(data); }, 'text'); }); }); \$.ajax({ url: './test.pl', data: { 'myString' : "#cont" }, success: function(data, textStatus, jqXHR) { alert('string saved to file'); } }); }; my $q = new CGI; print $q-&gt;header; print $q-&gt;start_html( -title =&gt; "Read a File", -style =&gt; {-src =&gt; 'css/ui-lightness/jquery-ui-1.10.3.custom.css" rel="stylesheet'}, -script =&gt; [ {-src =&gt; 'js/jquery-1.9.1.js'}, {-src =&gt; 'js/jquery-ui-1.10.3.custom.js'}, ], ); print $q-&gt;start_form; print $q-&gt;textfield( -style =&gt; 'font-family:verdana;width:300px;font-size:13px', -id =&gt; 'container', -value =&gt; '', ); print $q-&gt;button( -id =&gt; 'readFile', -name =&gt; 'submit_form', -value =&gt; 'Read', ); print $q-&gt;textfield( -style =&gt; 'font-family:verdana;width:300px;font-size:13px', -id =&gt; 'cont', -value =&gt; '', ); print $q-&gt;submit( -id =&gt; 'writeFile', -name =&gt; 'submit_form', -value =&gt; 'Write', ); print $q-&gt;script($script); print $q-&gt;end_html; test.pl use CGI (); my $cgi = CGI-&gt;new; print $cgi-&gt;header; my $string = $cgi-&gt;param("myString"); open (FILE, "&gt;", "./write.txt") || die "Could not open: $!"; print FILE $string; close FILE; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload