Solved

CGI beginner - why no refresh?

Posted on 2002-07-03
8
358 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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
 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

820 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