CGI beginner - why no refresh?

I think I may under a misapprehension as to what CGI is all about.
I have debugged and debugged my perl software down to a point wherby it is a simple item that is going wrong. (The bones of the code are below)

Why does this code not give me "Fred" in my text area? I know that the correct path is being followed when I press the button as the printing of the string "$MyString" at the end of the code gives me the correct printout - it just doesn't refresh my text area.

I was under the apprehension that if I send a whole new web page, this would replace what was on the screen but it doesn't seem to.
What am I missing when it comes to CGI?
How do I refresh what is on my screen?

Many thanks from a CGI beginner.

use CGI;
$co = new CGI;

if ( ! $co->param())
{
  $MyString = "Hello";
  DrawPage();
}
else
{
  if ($co->param('MyButton') ne "")
  {
    HandleRequest();
    DrawPage();
  }
}

sub HandleRequest
{
  $MyString="Fred";
}

sub DrawPage
{
  print
        $co->header(),
      $co->start_html
      (
      ),
      $co->start_form(),

          $co->textarea
          (
            -readonly=>'true',
            -name=>'MyArea',
            -default=>$MyString,
            -rows=>10,
            -columns=>45
          ),
           $co->submit
            (
              -name=>'MyButton',
              -value=>'Button'
            ),
      $co->end_form,
      "$MyString",
      $co->end_html;
 }
GSD4MEAsked:
Who is Participating?
 
VEngineerConnect With a Mentor Commented:
I found a solution, but not a root cause.  To solve this problem, you need to add one more parameter into your textarea function call:

$co->textarea
     (
     -readonly=>'true',
     -name=>'MyArea',
     -default=>$MyString,
     -rows=>10,
     -columns=>45,
     -override=>1
     ),


This is where I read about it:
http://stein.cshl.org/WWW/CGI/#overriding

It's *not* something that shows up in the HTML source for some reason.  I'm not exactly sure what's happening behind the scenes, but if I find out, I'll pass word along.  Another tip from a former beginner -- use strict; in every script you write and explicitly declare every variable you use.  It will save you countless headaches.  Here's how I'd write your script:

use strict;
use CGI;
use CGI::Pretty;

# CGI::Pretty makes the HTML source readable for debugging

my $co = new CGI;

my $MyString = "";

if (! $co->param()) {
      $MyString = "Hello";
      DrawPage();
}
elsif ($co->param('MyButton') ne "") {
      HandleRequest();
      DrawPage();
}


sub HandleRequest {
      $MyString = "Fred";
}

sub DrawPage {
      print $co->header(),
         $co->start_html(),
         $co->start_form(),

         $co->textarea(
              -readonly=>'true',
              -name=>'MyArea',
              -default=>$MyString,
              -rows=>10,
              -columns=>45
              -override=>1),

         $co->submit(-name=>'MyButton', -value=>'Button'),
         $co->end_form(),
         $co->end_html;
}
0
 
samriCommented:
Strange too.

Added $MyString in the title, it appears to be Fred.
     $co->start_html
     (-title=>$MyString
     ),

Somehow, it the textbox, it's Hello again.

If we change the $co->param(-name->MyArea,-value=Fred), then it works.

-- modified sub.
sub HandleRequest
{
 $MyString="Fred";
 $co->param(-name=>MyArea,value=>Another);
}

I still can't explain what is happening.  I would have suspected that the Assignment is not happening, but the title page show the Fred.  
0
 
samriCommented:
Much cleaner way, also work.

sub HandleRequest
{
 $MyString="Fred";
 $co->param(-name=>MyArea,value=>$MyString);
}
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
lexxwernCommented:
the pont is most cgi programmers rely too mch on the CGI module, if you are familiar with html, why not do it direct like this..


#!/usr/bin/perl -w

print "Content-type: text/html\n\n";

print qq(
<input type="text" value="fred, john and mary"/>
);

0
 
samriCommented:
lexxwern,

off topic:  I have the same thought before.  Now started to learn CGI.pm.  Personally, which do you prefer; "homemade" modules (ala CGI.pm), or the CGI.pm that comes with perl.

gsd4me: sorry to *hijack* your thread for a while. :)

cheers.
0
 
lexxwernCommented:
<offtopic>
me having good expertise in html, dont use any modules, it gives me the "html freedom" that i like :)
so i cant comment on this, sorry.
</offtopic>
0
 
GSD4MEAuthor Commented:
VEngineer
you're a star!
The fix works and everything is now hunky-dory!

Before I close this question, one little point please.

The form that this whole question relates to accesses its
own data files under my cgi-bin directory.
No-one else (apart from me uploading and downloading those data files) can see the data files.
What permissions do I need to set on the data files?
DO I need to give NO access to everyone else except the owner?
Lots of people will be accessing the data files (via the program) and they will require to read from and write data to the files which is only done VIA THE PROGRAM.
Does this affect the access permissions?
0
 
GSD4MEAuthor Commented:
Just clocing this question - beebn open for far too long
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.