Solved

CGI beginner - why no refresh?

Posted on 2002-07-03
8
354 Views
Last Modified: 2013-12-25
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;
 }
0
Comment
Question by:GSD4ME
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 15

Expert Comment

by:samri
ID: 7129472
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
 
LVL 15

Expert Comment

by:samri
ID: 7129476
Much cleaner way, also work.

sub HandleRequest
{
 $MyString="Fred";
 $co->param(-name=>MyArea,value=>$MyString);
}
0
 
LVL 2

Accepted Solution

by:
VEngineer earned 50 total points
ID: 7132711
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
 
LVL 12

Expert Comment

by:lexxwern
ID: 7133503
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 15

Expert Comment

by:samri
ID: 7133719
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
 
LVL 12

Expert Comment

by:lexxwern
ID: 7133775
<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
 

Author Comment

by:GSD4ME
ID: 7142813
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
 

Author Comment

by:GSD4ME
ID: 11293053
Just clocing this question - beebn open for far too long
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now