Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    31
    Rep Power
    1

    Read and write into a file


    What i ama trying to do is to read text from a file and display it into a textfield or write text into a file through a texfield. The function that read the file is ok but i cant write to a file.
    Code:
       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-->
    Code:
    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;
    WHAT MAY BE THE PROBLEM?
    THANK YOU!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    What error message do you get?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    31
    Rep Power
    1
    no error. It jus displays the alert "string saved to file" but when i open the file it's empty
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    The starting point would be to get rid of all of the ajax and javascript code and do a simple html page that submits to the script.

    Add this line near the beginning of the test.pl script.
    Code:
    use CGI::Carp qw(fatalsToBrowser);
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    31
    Rep Power
    1
    i just added it and nothing changed. please could you modify it, because i don't know how to do it without javascript
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser);
    
    my $cgi = CGI->new;
    
    print $cgi->header,
          $cgi->start_html,
          $cgi->start_form(-action=>'/cgi-bin/test.pl'),
          $cgi->textfield(-name=>'myString'),
          $cgi->submit,
          $cgi->end_form,
          $cgi->end_html;
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser);
    
    my $cgi    = CGI->new;
    my $string = $cgi->param("myString");
    
    print $cgi->header, $cgi->start_html('Writing to a file');
    
    open my $fh, '>', './write.txt' or die "failed to open './write.txt' $!";
    print $fh $string;
    close $fh;
    
    print $cgi->p( qq(write.txt should contain "$string") );
    print $cgi->end_html;
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    31
    Rep Power
    1
    Server error!

    The server encountered an internal error and was unable to complete your request.

    Error message:
    couldn't create child process: 720002: test.pl

    If you think this is a server error, please contact the webmaster.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    31
    Rep Power
    1
    im working with xampp. And i put test.pl into:C:\xampp\cgi-bin
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    You need to change the shebang line, which is the first line in the script, to point to where perl.exe is located. You also need to make sure that the path in the form's action attribute is correct.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    31
    Rep Power
    1
    'C:\xampp\cgi-bin\test.pl' this is the path , and i changed the first line of the code , now the error doesn't appear but the file write.txt is still empty. when i click 'submit' doesn't happen nothing
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    31
    Rep Power
    1
    Not allowed to load local resource: file:///C:/xampp/cgi-bin/test.pl

    this is what console shows
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    when i click 'submit' doesn't happen nothing
    Please be more descriptive.

    Do you get a blank page?

    Do you get the "write.txt should contain ..." line?

    Not allowed to load local resource: file:///C:/xampp/htdocs/perl/data19/test.pl
    Your perl script is in the wrong directory. It should be in the cgi-bin dir, or whatever dir your apache ScriptAlias directive is configured to point to.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    31
    Rep Power
    1
    sorry i made a mistake and editet my comment. When i click 'submit' doesnt happen nothing just the text i wrote stays in the textfield and console shows:
    Not allowed to load local resource: file:///C:/xampp/cgi-bin/test.pl
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    31
    Rep Power
    1
    i just solved the problem it should be action=>'http://localhost/cgi-bin/test.pl
    THANK YOU VERY MUCH FOR YOUR HELP
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    31
    Rep Power
    1

    Perl-Hash table


    Hi! I am trying to do is to print hash values that have the same key type. For example all values that start with lbl. For this i need a switch case but the way i do it doesn't work.
    Code:
    #!"C:\xampp\perl\bin\perl.exe"
    
    print "Content-type: text/html; charset=iso-8859-1\n\n";
    package shembull;
    $rhash= {
        lbl_emri    =>  "Lau",
        lbl_mbiemer =>  "xha",
        txt_emri    =>  "emri",
        txt_mbiemri =>  "mbiemri",
        btn_submit  =>  "submit",
    }
    
    sub label_input {
    my $value = @_;
    return "<label >value</label>";
    }
    
    sub txt_input {
    my $value = @_;
    return "<textarea >value</textarea>";
    }
    
    sub btn_input {
    my $value = @_;
    return"<button type="button">value</button>";
    }
    
    foreach $tmp (@{$rhash}){
       switch{
        if(keys $rhash =~/$lbl/){
            print label_input;
        }}}
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo