Pass textbox input to perl script on server - perl

Sorry in advance for a potentially dumb newbie question, but here goes.
I am learning web app programming and I would like to have an input textbox on my webpage where the user enters some text. Then I capture that text and pass to a perl script which generates some output. I then take this text output and pass it back to the webpage.
Can someone point me in the right direction on how to do this.
Can be a really simple example, where the user inputs some text. I take the text and pass to a perl script which turns everything to uppercase - uc() - and then passes back to the webpage.
Thanks

In your html body:
<FORM ACTION="/cgi-bin/results.pl">
<P>Enter a value: <INPUT NAME="value">
<P><INPUT TYPE="SUBMIT" VALUE="Next">
</FORM>
In your results.pl:
use CGI qw(:standard);
my $value = uc(param('value'));
print header;
print start_html;
print p($value);
print end_html;

The page needs to contain a form. The action attribute of the form needs to point to a URL that your webserver will process with the Perl program. The simplist way to achieve this is using CGI, a more modern approach uses PSGI. Most Perl form processing libraries use a similar interface to CGI.pm's

useCGI;
my $q = CGI->new;
my $text_box_value = $q->param( 'my_text_box_name' );
This is a decent CGI tutorial: http://www.tutorialspoint.com/perl/perl_cgi.htm . Or there's this http://www.cgi101.com/book/ or this http://www.lies.com/begperl/ or this http://websitehelpers.com/perl/ all found here: http://www.google.com/search?q=perl+CGI+tutorial

Related

WWW::Mechanize Extraction Help - PERL

I'm try to automate the extraction of a transcript found on a website. The entire transcript is found between dl tags since the site formatted the interview in a description list. The script I have below allows me to search the site and extract the text in a plain-text format, but I'm actually looking for it to include everything between the dl tags, meaning dd's, dt's, etc. This will allow us to develop our own CSS for the interview.
Something to note about the page is that there are break statements inserted at various points during the interview. Some tools we've found that extract information from webpages using pairings have found this to be a problem since it only grabs the information up until the break statement. Just something to keep in mind if you point me in a different direction. Here's what I have so far.
#!/usr/bin/perl -w
use strict;
use WWW::Mechanize;
use WWW::Mechanize::TreeBuilder;
my $mech = WWW::Mechanize->new();
WWW::Mechanize::TreeBuilder->meta->apply($mech);
$mech->get("http://millercenter.org/president/clinton/oralhistory/madeleine-k-albright");
# find all <dl> tags
my #list = $mech->find('dl');
foreach ( #list ) {
print $_->as_text();
}
If there is a tool that essentially prints what I have, only this time as HTML, please let me know of it!
Your code is fine, just change the as_text() method to as_HTML() and it will show the content with HTML tags included.

PERL CGI mailto does not work

I have a web report written in PERL CGI. It pulls some constantly changing data from a flat-file DB and displays the current status in a table on the web page. I want to be able to click a link that will push all of that data into an email that can be edited before sending.
This is what I have as my last chunk of HTML on the page. The "Go To Status" link works but the mailto:xxx#xx.com link causes server errors. Does "mailto" not work in a CGI script for some reason? It gets rendered as HTMl so I'm not sure why it wouldn't.
sub EndHtml {
print "<P align=right> <a href='http://www.xxx.com/~a0868183/cgi-bin/xxx.cgi'>Go to Status</a> </p>\n";
print "<p align=right> <a href='mailto:xxx#xx.com'></a>Send EOS</p>\n";
print "</BODY></HTML>\n";
}
(Once I figure this out I will then put the variables with the data into the email)
Thanks,
Jared
# has special meaning in a double quote delimited string.
Always start your script with:
use strict;
use warnings;
Then you will get alerted (if you read your log files):
Possible unintended interpolation of #xx in string
Then you can escape it:
mailto:xxx\#xx.com
Or use a single quoted string:
print q{<p align=right> <a href='mailto:xxx#xx.com'></a>Send EOS</p>\n};
Or don't embed your HTML in the middle of your Perl and use a Template language (like Template Toolkit).
You probably want to put some content in the anchor too.

Linking pages with CGI scripts

I am trying to write a survey that displays only one question at a time, each on a separate page. I cannot figure out how to have my CGI file accomplish this, right now I have everything on one page but want my user to be able to hit a "next" button to bring them to a new question. I am trying to do this with Perl and HTML exclusively. for example:
use CGI qw(:standard); # Include standard HTML and CGI functions
use CGI::Carp qw(fatalsToBrowser); # Send error messages to browser
#
# Start by printing the content-type, the title of the web page and a heading.
#
print header, start_html("Hello"), h1("Hello");
if (param()) { # If true, the form has already been filled out.
$who = param("myname");
$cats = param("cats"); # Extract the value passed from the form.
if($cats == "Y"){
print p("Hello, your name is $who, and you like cats");
}
else{
print p("Hello, your name is $who, and you don't like cats"); # Print the name.
}
}
else { # Else, first time through so present form.
print start_form();
print p("What's your name? ", textfield("myname"));
print p("Do you like cats? Y for yes and N for no", textfield("cats"));
print p(submit("Submit form"), reset("Clear form"));
print end_form();
}
print end_html;
If I want the cats question to appear on the next page, by taking out the submit button and putting in one that functions like a next, do I have to link the button to another page or can that be achieved in one script? So in short, can you create and link multiple html pages to run a survey with just one cgi script?
Sure you can. The problem is that your script needs to know which page the user has just submitted in order to know which page to show next. However, that can be achieved easily with a hidden <input> inside your <form>s. Such hidden inputs are sent by the browser to the CGI script just as if they were regular inputs, e.g. text inputs, drop-down boxes or checkboxes.
On the server side this hidden <input>s name and value are available via the normal param() method call. The hidden input itself is created with hidden(); please read the documentation available for it.

I want to check if number starts with 4 or 5 in CGI

I need to write some script in CGI which is new to me. I am trying to do if else with condition numbers starting with 5 or 6. So do one code if number starts with 5 and do another if number starts with 6.
use 5.013;
use warnings;
use Scalar::Util qw( looks_like_number );
use CGI;
my $param = CGI->new()->param('some_example');
given (substr $param, 0, 1) {
when (! looks_like_number($_) ) { say 'Not a number' }
when (5) { say 'starts with 5' }
when (6) { say 'starts with 6' }
}
Alternatively, rather than using substr to get the first letter, put $param and change (5) to your regex of choice.
I don't think you understand what CGI is. CGI is simply a set of environment variables that are set up by the webserver, and your program is executed with them. The output of the program becomes the webpage.
So if you want to write a CGI script in Python, PHP, C, Assembly, Whitespace... as long as it can be called and use environment variables, it's fine.
So this is really a language question. Which language are you using?
EDIT You specified Perl in a comment to this answer. I suggest you edit the question.
What's your input number? The Perl script will be run with a whole truckload of extra environment variables. Two of the most important are QUERY_STRING and REQUEST_METHOD. CGI consists of a specification of these environment variables, so any language can be used to write CGI.
Consider perl_cgi.cgi?something=else. The bit following the ? is the QUERY_STRING. You can specify this directly as part of an anchor:
Run with something equals else
or as part of a form (one of GET or POST, defaults to GET):
<form action="perl_cgi.cgi" method="[GET or POST]">
<input type="text" name="something" value="else"/>
<input type="submit" value="Submit!"/>
</form>
This will run your program with the same query string as above (or a different parameter, if the text box is changed) but REQUEST_METHOD will be either GET or POST depending.
So let's write a Perl CGI script to print the first number of the string we get (we're only passed strings):
use CGI;
$cgi=new CGI;
$x=$cgi->param('x');
$firstnum=substr($x, 0, 1);
print "Content-type: text/html\n\n";
print <<"EOF";
<html>
<head>
<title>My sample HTML page</title>
</head>
<body>
<p>The first number of $x is $firstnum</p>
</body>
</html>
EOF
This presupposes that this program is run as [program_name]?x=[some string]. It's up to you to make sure that's the case.
That should give you enough. You can check firstnum to see if its 5 or 6, then do different things depending.

WWW::Mechanize and "HTTP::Message content must be bytes at..."

I'm writing simple program which has to change some data on Polish auction site.
One of the steps involves loading edit page, changing one value, and submitting it.
Sample page can be viewed here: http://depesz.com/various/new_item.php.html - this is just static copy of such edit page.
Relevant part of my perl code:
$agent->form_number( 1 );
$agent->submit();
$agent->form_number( 1 );
my $q = $agent->current_form()->find_input( 'scheme_id' );
$agent->field('scheme_id', '1025');
# $agent->field('description', encode('utf-8', $agent->value("description")));
# $agent->field('location', encode('utf-8', $agent->value("location")));
# $agent->field('transport_shipment_description', encode('utf-8', $agent->value("transport_shipment_description")));
$agent->submit;
print $agent->response->decoded_content . "\n";
After first submit I get the page I showed. Then I change value in scheme_id field to 1025, and submit the form.
Afterward I get:
HTTP::Message content must be bytes at /usr/local/share/perl/5.8.8/HTTP/Request/Common.pm line 91
I tried to recode values on text fields on the form - hence the agent->field(... encode) lines, but it didn't help.
At the moment I have no idea what on the form can make WWW::Mechanize fail in such way, but I clearly cannot fix in on my own.
Is there any way to debug this situation? Or perhaps I should do something differently?
Make sure your LWP and WWW-Mechanize modules are fully up to date. LWP fixed a number of encoding problems in late 2008, if I recall correctly.
I have the same problem.
Solved it with :
my $newcontent = encode('utf-8', $file);
before posting the content!
thanks,
mike
see http://www.perlmonks.org/?node_id=647935