Solved

CGI beginner - why no refresh?

Posted on 2002-07-03
8
355 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
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…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 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

12 Experts available now in Live!

Get 1:1 Help Now