42 users online (0 members and 42 guests)  


  Results 1 to 6 of 6

Related

  1. Problem with excel file writing using PERL    Forum: CGI Perl Forum
    Replies: 0
  2. Replies: 0
  3. Replies: 3
  4. Replies: 1
  5. Email Text File    Forum: CGI Perl Forum
    Replies: 1
  1. #1
    long123's Avatar
    New User

    Status
    Offline
    Join Date
    Jul 2005
    Posts
    9

    Writing to text file problem

    Hi every one. I have this problem that need your help. Currently, I am creating a web page that consists of 4 check buttons named A,B,C,D and a submit button. What i want is that when I tick to check button, let say I tick button A,D the moment i click submit, the information will be save into a text file called parameter.txt that " You have clicked button A,D. Please give me the code how to do it or modify my code. I tried to do it but can not get it done.
    Code:
    #!/usr/bin/perl
    use CGI qw(:standard); 
    use CGI; 
    $q=new CGI;
    
    my $error;# variable to store error user selected
    $error = param('error_type'); # get error from "submit" button
    
    #my ($char1);#, $char2); 
    #$char1 = substr($error,0,3); 
    #$char2 = substr($error); 
    
    open (FILE, " parameter.txt") or die "no such file"; # create a new text file
    print FILE  "Error chosen:  $error"; # write selected frequency into the text file
    close (FILE); # close filehandle
    
    #system("/home/thuy/public_html/cgi-bin/EP2");# > /home/thuy/public_html/cgi-bin/log1.txt"); # run the C++ program
    
    print $q ->header; # start a HTML webpage
    
    print $q ->img ({-src=>'mda1.jpg', # print the MDA logo
       -width=>'150',
       -height=>'70',
       -border=>'0'}); 
    
    print $q ->start_html (-title=>'Error Chosen', # name of webpage
         -BGCOLOR=>'#FFFFFF', # background colour of webpage
         -text=>'#000000', # colour of text 
         -BACKGROUND=>'webground_lightcolors_000559.jpg'); # background of webpage 
    
    print $q ->br; # a line break 
    print $q ->br; 
    print $q ->font ({-size=>6, # set font size
       -color=>'#339933'}, "The error you have chosen to monitor : $error"); # set font colour
    
    print $q ->br;
    print $q ->br; 
    
    
    print $q ->br;
    print $q-> start_form(-method =>'POST',
       -action =>"http://localhost/~thuy/cgi-bin/startex1.pl ",
       -target =>'');
    print  $q->submit (-name=>'Submit button',
                 -value=>'Start');
    print  $q->end_form; 
    
    print $q-> start_form(-method =>'POST',
       -action =>"http://localhost/~thuy/cgi-bin/Perl_button.pl ");
    print  $q->submit (-name=>'Submit button',
                 -value=>'Back');
    print  $q->end_form; 
    
    print $q ->br; 
    print $q ->br; 
    print $q ->br; 
    print $q ->img({-src=>'tp_logo.gif', # print the TP logo
       -width=>'150',
       -height=>'25',
       -border=>'0'});  
    
    print $q ->end_html, "\n"; # end the HTML webpage

  2. #2
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: Writing to text file problem

    that code can be modified to do what you want, but is not verry efficent... try this:
    Code:
    #!/usr/bin/perl
    print "Content-type: text/html \n\n";
    if(!param['submitbutton']){
    print "<html>
    <body>
    <form method=\"POST\" action=\"$ENV{'REQUEST_URI'}\">
    	<input type=\"checkbox\" name=\"chk1\" value=\"ON\">1<br>
    	<input type=\"checkbox\" name=\"chk2\" value=\"ON\">2<br>
    	<input type=\"checkbox\" name=\"chk3\" value=\"ON\">3<br>
    	<input type=\"checkbox\" name=\"chk4\" value=\"ON\">4<br>
    	<input type=\"submit\" value=\"Submit\" name=\"submitbutton\">
    </form>
    </body>
    </html>";
    }elsif{
    open (FILE, ">parameter.txt") or die "no such file";
    print FILE "The last user to submit form returned the following results:\ncheckbox 1: param['chk1']\ncheckbox 2: param['chk2']\ncheckbox 3: param['chk3']\ncheckbox 4: param['chk4']";
    close(FILE);
    print "You returned the following results:<br>checkbox 1: param['chk1']<br>checkbox 2: param['chk2']<br>checkbox 3: param['chk3']<br>checkbox 4: param['chk4']";
    }
    1;
    i dont think there is an error, but i did not test it so there may be one, if there is just reply with the error.
    Last edited by ALL; 07-28-2005 at 01:07 PM.

  3. #3
    long123's Avatar
    New User

    Status
    Offline
    Join Date
    Jul 2005
    Posts
    9

    Re: Writing to text file problem

    Thank you for your reply, I tried but there are some error. This is the code that I had modified and the error occur just before elsif line, it was indicated that error near there but it did not say what error is it. Can you help me to check your code again. By the way I would like to tell you this. I already created a wed page that includes 4 checkboxes 1,2,3,4, and a submit button as well. What i am asking your help is that, when I clicked submit button, it will be linked to another page let call it page2.pl. The moment i click submit button, the information about which checkbox was check will be write to a text file called parameter.txt, and at the same time, when I click submit button, I will be brought to page2.pl. In that page, there is a sentence: "You have chosenchek box1 .......). Thank you for spending time to solve my problem.This is ur code that I had modified
    Code:
    #!/usr/bin/perl
    
    print "Content-type: text/html \n\n";
    if(!param ('error_type'))
    {
    print "<html>
        <body>
        <form method=\"POST\" action=\"$ENV{'REQUEST_URI'}\">
            <input type=\"checkbox\" name=\"chk1\" value=\"ON\">1<br>
            <input type=\"checkbox\" name=\"chk2\" value=\"ON\">2<br>
            <input type=\"checkbox\" name=\"chk3\" value=\"ON\">3<br>
            <input type=\"checkbox\" name=\"chk4\" value=\"ON\">4<br>
            <input type=\"submit\" value=\"Submit\" name=\"submitbutton\">
    </form>
    
    </body>
    </html>";
    }
    else if
    {
    open (FILE, "parameter.txt") or die "no such file";
    print FILE "The last user to submit form returned the following results:\ncheckbox 1: param['chk1']\ncheckbox 2: param['chk2']\ncheckbox 3: param['chk3']\ncheckbox 4: param['chk4']";
    close(FILE);
    print "You returned the following results:<br>checkbox 1: param['chk1']<br>checkbox 2: param['chk2']<br>checkbox 3: param['chk3']<br>checkbox 4: param['chk4']";
    };

  4. #4
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: Writing to text file problem

    sorry, this code "should" work, but i cant get it to work on my server, i think it is because i cannot upload it in ascii mode, so make sure when you upload it (if you need to upload it) you upload it in ascii mode.

    Code:
    #!/usr/bin/perl
    print "Content-type: text/html\n\n";
    if(!param('submitbutton')){
    print "<html>
    <body>
        <form method=\"POST\" action=\"$ENV{'REQUEST_URI'}\">
            <input type=\"checkbox\" name=\"chk1\" value=\"ON\">1<br>
            <input type=\"checkbox\" name=\"chk2\" value=\"ON\">2<br>
            <input type=\"checkbox\" name=\"chk3\" value=\"ON\">3<br>
            <input type=\"checkbox\" name=\"chk4\" value=\"ON\">4<br>
            <input type=\"submit\" value=\"Submit\" name=\"submitbutton\">
    </form>
    </body>
    </html>";
    }else{
    open(FILE, ">parameter.txt") || die "no such file";
    print FILE "The last user to submit form returned the following results:\ncheckbox 1: ".param('chk1')."\ncheckbox 2: ".param('chk2')."\ncheckbox 3: ".param('chk3')."\ncheckbox 4: ".param('chk4');
    close(FILE);
    print "You returned the following results:<br>checkbox 1: ".param('chk1')."<br>checkbox 2: ".param('chk2')."<br>checkbox 3: ".param('chk3')."<br>checkbox 4: ".param('chk4');
    }
    i used a script debugger located here (to debug it):
    http://ult-tex.net/tools/ultra/line_count.pl

  5. #5
    long123's Avatar
    New User

    Status
    Offline
    Join Date
    Jul 2005
    Posts
    9

    Re: Writing to text file problem

    There is one error. The error was indicated that at line "if(!param('submitbutton')){", they said that undifine subroutine and main:: param call

  6. #6
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: Writing to text file problem

    then try changing this:
    Code:
    if(!param('submitbutton')){
    to:
    Code:
    if(param('submitbutton') != ''){



Tags for this Thread