CGI script problem

P

Paul F. Johnson

Hi,

I have a very simple form which is supposed to take an entry from the
webpage form and then redirect accordingly.

The form looks like this

<form method="POST" name="cgi/articles.cgi">
<select>
<option value="#" selected>Choose an article...</option>
<option value="overview">Overview</option>
<option value="defining">Defining 'Community' Arts</option>
<option value="whybigisbeautiful">Why Big is Beautiful</option>
<option value="whybigisn't">Why Big isn't Always Beautiful</option>
<option value="whyfilm">Why Film?</option>
<option value="processversusproduct">Process Versus Product</option>
<option value="devisingmethodologies">Devising Methodologies</option>
<option value="leightech">Using Mike Leigh's Techniques</option>
<option value="workingwithnon-actors">Working with Non-Actors</option>
<option value="funding">Funding and Political Perspectives</option>
<option value="conclusions">Conclusions</option>
<option value="authors">About the Authors</option>
</select>
<input type="submit" value="Go!">
</form>

with the cgi script looking like this

#!/usr/bin/perl -wT
use CGI;
use CGI::Carp qw(fatals_to_browser);
use strict;
my($q) = new CGI;
print q->header("Content type: text/html\n\n");
read (STDIN, $input,$ENV{'CONTENT_LENGTH'});
if ($input eq '#')
{
print q->header("Content type: text/html\n\n");
exit;
}
$url = $input + '.html';
print $q->redirect($url);

This should do the redirection for me when someone clicks on the "Go!"
button (unless I've completely messed up somewhere [which is not unknown
;-p]).

I'm trying to rewrite something called cssscriptdict - it's something
revolting you can see if you go to
http://www.smmp.salford.ac.uk/dixonpeters. It looks like an unholy mix
of CSS and JS - I'm trying to vanquish the JS and these scripts.

This cssscriptdict stuff is only ever used for the same dropdown menu on
a number of pages. If I can change it to a #include which can use the
cgi script, then life will be happier (well, it will for me).

So, is the CGI wrong, the form wrong or both?

TTFN

Paul
 
J

Jeff Thies

Paul said:
Hi,

I have a very simple form which is supposed to take an entry from the
webpage form and then redirect accordingly.

The form looks like this

<form method="POST" name="cgi/articles.cgi">
<select>
<option value="#" selected>Choose an article...</option>
<option value="overview">Overview</option>
<option value="defining">Defining 'Community' Arts</option>
<option value="whybigisbeautiful">Why Big is Beautiful</option>
<option value="whybigisn't">Why Big isn't Always Beautiful</option>
<option value="whyfilm">Why Film?</option>
<option value="processversusproduct">Process Versus Product</option>
<option value="devisingmethodologies">Devising Methodologies</option>
<option value="leightech">Using Mike Leigh's Techniques</option>
<option value="workingwithnon-actors">Working with Non-Actors</option>
<option value="funding">Funding and Political Perspectives</option>
<option value="conclusions">Conclusions</option>
<option value="authors">About the Authors</option>
</select>
<input type="submit" value="Go!">
</form>

with the cgi script looking like this

#!/usr/bin/perl -wT
use CGI;
use CGI::Carp qw(fatals_to_browser);
use strict;
my($q) = new CGI;
print q->header("Content type: text/html\n\n");

You've just printed a header. The blank line, the second \n signifies
the end of the header.

read (STDIN, $input,$ENV{'CONTENT_LENGTH'});

if ($input eq '#')
{
print q->header("Content type: text/html\n\n");
exit;

Another header!
}
$url = $input + '.html';
print $q->redirect($url);

Just read the perl docs on CGI.pm.

Google: perldoc CGI

Or if you have perl on you machine, type that at a command prompt.


What you'll want to do is name your select:

<select name="url">

then:

use CGI;
my $q=new CGI;

my $url=$q->param('url');

unless($url eq '#'){
print $q->redirect($url . '.html');

}
This should do the redirection for me when someone clicks on the "Go!"
button (unless I've completely messed up somewhere [which is not unknown
;-p]).

I'm trying to rewrite something called cssscriptdict - it's something
revolting you can see if you go to
http://www.smmp.salford.ac.uk/dixonpeters. It looks like an unholy mix
of CSS and JS - I'm trying to vanquish the JS and these scripts.

This cssscriptdict stuff is only ever used for the same dropdown menu on
a number of pages. If I can change it to a #include which can use the
cgi script, then life will be happier (well, it will for me).

So, is the CGI wrong, the form wrong or both?

TTFN

Paul
 
P

Paul F. Johnson

Hi,

Jeff said:
What you'll want to do is name your select:

<select name="url">

then:

use CGI;
my $q=new CGI;

my $url=$q->param('url');

unless($url eq '#'){
print $q->redirect($url . '.html');

}

For some reason, on my access_log, all I'm seeing is the following for
that script...

127.0.0.1 - - [11/Oct/2004:12:14:23 + 0100] "POST
/smmp-new/research/performance/dixonpeters/thearticlespage.html
HTTP/1.1" 200 3568
"http://127.0.0.1/smmp-new/research/performance/dixonpeters/thearticlespage.html"
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7) Gecko/20040626
Firefox/0.9.1

That looks to me that it's posting out the referring page rather than
the page contained in the option value setting.

My httpd.conf file looks fine (and I do have cgi scripts running on it)

TTFN

Paul
 
J

Jan Faerber

Jeff said:
Paul F. Johnson wrote:

When is it 'articles.cgi' and when 'articles.pl'?
....

When do you use brackets - 'my($q) = ...'?
And what is 'q': 'print q->header(...'?
 
M

mbstevens

Paul said:
read (STDIN, $input,$ENV{'CONTENT_LENGTH'});

Verrrrrrrry strange to try reading from STDIN in a CGI script
to get its data from a user.
You have a form that's creating option data.
In your script you will be using
param(..........) to access the data from that form.

Try to get hold of Elizabeth Castro's
"Perl and CGI for the World Wide Web"
to bring it all together. I also have links
to various sites that have tutorials at
http://www.mbstevens.com/bc.html ...and pull
down form menu and choose CGI/Perl.
 
J

Jeffrey Silverman

So, is the CGI wrong, the form wrong or both?

I haven't looked in detail. However the form is certainly wrong a little
bit at least: No "action="!

Change

<form method="POST" name="cgi/articles.cgi">

to

<form method="POST" action="cgi/articles.cgi">

Assuming, of course, that the name and path of the CGI script is
"cgi/articles.cgi"

There may as yet be problems with the CGI script itself. I didn't look any
deeper as the fundamental problem is that none of the data is getting to
the script!
 
J

Jeff Thies

Paul said:
Hi,




For some reason, on my access_log, all I'm seeing is the following for
that script...


That script has no error checking, a path to perl, or a default if the
option is "#". Add them.

Also, your html paths in your options are relative. So redirects would
be done relative to the script directory.

print $q->redirect('path_to_html' . $url . '.html');

or use a more qualified path in your options.

Jeff

127.0.0.1 - - [11/Oct/2004:12:14:23 + 0100] "POST
/smmp-new/research/performance/dixonpeters/thearticlespage.html
HTTP/1.1" 200 3568
"http://127.0.0.1/smmp-new/research/performance/dixonpeters/thearticlespage.html"
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7) Gecko/20040626
Firefox/0.9.1

That looks to me that it's posting out the referring page rather than
the page contained in the option value setting.

My httpd.conf file looks fine (and I do have cgi scripts running on it)

TTFN

Paul
 
R

Rob McAninch

Jeff Thies
You've just printed a header. The blank line, the second \n
signifies the end of the header.

print $q->header; # will do the same thing, the extra \n's are not
necessary and probably inject extra blank lines into the document
for no purpose.
 
R

Rob McAninch

Jan Faerber said:
When is it 'articles.cgi' and when 'articles.pl'?

Depends on how you have your server configured. *.cgi is more
common, while *.pl was typically used for perl library files prior
to perl modules.
Isn't it '<form method="post" action="/cgi...">'?

The action isn't necessary if it submits to itself.
...

When do you use brackets - 'my($q) = ...'?

In this case the parens around $q is unnecessary. You might do
something like:

my( @array, %hash) = (); # both are initialized empty.

I.e., use parens when the my should include more than one variable,
or if you are trying to force a list context on whatever is right
of the = sign.
And what is 'q': 'print q->header(...'?

Obviously a typo. $q.
 
R

Rob McAninch

mbstevens said:
Try to get hold of Elizabeth Castro's
"Perl and CGI for the World Wide Web"
to bring it all together.

Careful with that, it doesn't initially use the CGI module for
parsing forms but rather a home rolled version.
 
N

NOXwebmasterx

Rob said:
Careful with that, it doesn't initially use the CGI module for
parsing forms but rather a home rolled version.
She mentions this herself in the *second* edition, which
I should also have mentioned. The first edition is outdated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top