1

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, read.txt and write.txt, in the same folder as the code.

The two jQuery functions (below, with surrounding server-side Perl code) are:

<!-- language: perl -->

    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->header;
    print $q->start_html(
        -title => "Read a File",
        -style  => {-src => 'css/ui-lightness/jquery-ui-1.10.3.custom.css" rel="stylesheet'},
        -script => [
             {-src => 'js/jquery-1.9.1.js'},
             {-src => 'js/jquery-ui-1.10.3.custom.js'},
        ],
        );
    print $q->start_form;
    print $q->textfield(
        -style => 'font-family:verdana;width:300px;font-size:13px',
        -id => 'container',
        -value => '',
        );
    print $q->button(
        -id       => 'readFile',
        -name     => 'submit_form',
        -value    => 'Read',
        );

    print $q->textfield(
        -style => 'font-family:verdana;width:300px;font-size:13px',
        -id => 'cont',
        -value => '',
        );

    print $q->submit(
        -id       => 'writeFile',
        -name     => 'submit_form',
        -value    => 'Write',
        );
    print $q->script($script);
    print $q->end_html;

test.pl
use CGI ();
my $cgi = CGI->new;
print $cgi->header;
my $string = $cgi->param("myString");
open (FILE, ">", "./write.txt") || die "Could not open: $!";
print FILE $string;
close FILE;
Armida
  • 65
  • 1
  • 3
  • 11
  • 3
    you need a server side script to process your request and update the file – 19greg96 Jul 20 '13 at 12:30
  • can i do it in perl? where should i put this script? – Armida Jul 20 '13 at 12:32
  • http://perldoc.perl.org/CGI.html (I guess) – Felix Kling Jul 20 '13 at 12:35
  • justt edited my question there is the complete code. what should i add else? – Armida Jul 20 '13 at 12:41
  • Why are you using `qq` if you're not interpolating variables? You're needless adding the need to escape, and an appropriate HEREDOC would likely help to discern the JS from the Perl. It sounds like with your background you'd be better off working out the write piece in Perl using a standard request first (basic HTML form pointing to the script) and then adding in any kind of Ajax you may want after that piece is known to work. Watch out for XSS if you're displaying what the user enters. – Matt Whipple Jul 20 '13 at 13:24
  • do you have any example 'couse i ama new at programming in perl. thank you – Armida Jul 20 '13 at 13:28

1 Answers1

9

You can't write in files with Javascript/jQuery for security reasons because, if you could, a user would be able to change easily the file url to edit with his console and create consequent damages. For more details, you can have a look to this page.

In order to solve your problem, I think the best way to do is send to a remote php file, using ajax, the content of the files you want to update. Then, this file will do the update.

Lucas Willems
  • 6,673
  • 4
  • 28
  • 45
  • just edited my question there is the complete code. is there any other way that i can do it with perl ? – Armida Jul 20 '13 at 12:40
  • Yes I think but I don't work with Perl and I can't say exactly how to do. But I think you could have a look to this page : http://www.gossland.com/perlcourse/files/editing – Lucas Willems Jul 20 '13 at 12:43
  • what about an other way just with jQuery and html without using get and post? is it possible? thank you – Armida Jul 20 '13 at 12:47
  • 1
    No it's not. I explain you in my answer. You really want to be hacked ! – Lucas Willems Jul 20 '13 at 12:47
  • It is completely different. When you are on a website, you are read a page and it's not for this reason, you update it and save these modifications. Do you understand ? – Lucas Willems Jul 20 '13 at 13:17
  • somewhere i found this: If you want to do this without a bunch of server-side processing within the page, it might be a feasible idea to blow the text value into a hidden field (using PHP). Then you can use jQuery to process the hidden field value.-- does anyone know how to blow the text value into e hidden field – Armida Jul 20 '13 at 13:35
  • now i just edited again my code, but it still doesn't work. Please take a look – Armida Jul 20 '13 at 16:18