Textarea Tag in Perl - perl

I want to do a textarea/textbox in perl. I have this tag but it is coming up as syntax error.
<textarea name="answer" rows="20" cols="70"></textarea>
I have no idea why it is coming up, my code is:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use constant debug=>0;
print "Content-type: text/html\n\n";
# Program: assignment 3
# Author: Jay
# Date: 02/2014
my $cgi= CGI->new();
my $a=$cgi->param('action');
my $num1=$cgi->param('num1');
my $num2=$cgi->param('num2');
my $num3=$cgi->param('num3');
my $num4=$cgi->param('num4');
my $hard=$cgi->param('hardware');
my $soft=$cgi->param('software');
print "$a, $num1, $num2, $num3, $num4" if debug;
if ($a eq undef) {
print "
<!DOCTYPE html>
<html>
<head>
<title>Error Logging</title>
</head>
<body>
<h1>Error Log - IT Support</h1>
<form method=\"post\" action=/~it.jasonc/cgi-bin/assignment3.pl>
<input type=\"hidden\" name=\"action\" value=\"error\">
<table>
<tr><td>Site:</td><td><input type=\"text\" name=\"site\"></td></tr>
<tr><td>Type of Error:</td>
<td><select name=\"error\">
<option value=\"1\">Hardware</option>
<option value=\"2\">Software</option></select></td></tr>
<textarea name="answer" rows="20" cols="70"></textarea>
<tr><td colspan=\"2\"><input type=\"submit\" value=\"Submit Error\"></td></tr>
</table>
</form>
</body>
</html> "
}
if ($hard) {
print "Go to room 1";
}
if ($soft) {
print "Go to room 2";
}
I had another type as an example
<textarea type=\"text\" name=\"details\" value\rows="4" cols="50">
Please explain the error here!
</textarea>
Please help!!
Jay

If you quote a string using the double quote " character, you must escape all double quotes inside it. You have escaped some, but not all:
...
<option value=\"2\">Software</option></select></td></tr>
<textarea name="answer" rows="20" cols="70"></textarea>
# ^ ^ ^ ^ ^ ^
As you can see, \"2\" for example is escaped, but "answer" is not.
A better way to handle this string is to use a different quoting, such as using qq, which can take many different delimiters, according to your needs, for example qq##:
print qq#
<!DOCTYPE html>
....
You can also use a heredoc:
print <<EOF;
<!DOCTYPE html>
....
EOF

The line <textarea name="answer" rows="20" cols="70"></textarea> is inside a large double quoted string. If you put a double quote inside the string (a large multiline string) you have to escape it by putting a backslash before it. It's done everywhere in your code, except where your code breaks, where there are 6 unescaped double quotes.
As the following text is HTML and not perl, a syntax error is to be expected.
If you don't want to care too much about quoting double quotes (it's always easy to forget some), there is of course another way to do it (actually more than one).
You could for instance use qq{} instead of double quotes for perl strings. Then your code would work if quotes are escaped or not. Nothing special, it's perfectly standard perl.
Below some use exemple where I unescaped some random quote inside the HTML code (in production code I would probably unescape them all to avoid syntaxic nois).
Beside that the mere difference is the use of qq{ after print and replacing the double quote closing string by }
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use constant debug=>0;
print "Content-type: text/html\n\n";
# Program: assignment 3
# Author: Jay
# Date: 02/2014
my $cgi= CGI->new();
my $a=$cgi->param('action');
my $num1=$cgi->param('num1');
my $num2=$cgi->param('num2');
my $num3=$cgi->param('num3');
my $num4=$cgi->param('num4');
my $hard=$cgi->param('hardware');
my $soft=$cgi->param('software');
print "$a, $num1, $num2, $num3, $num4" if debug;
if ($a eq undef) {
print qq{
<!DOCTYPE html>
<html>
<head>
<title>Error Logging</title>
</head>
<body>
<h1>Error Log - IT Support</h1>
<form method="post" action=/~it.jasonc/cgi-bin/assignment3.pl>
<input type=\"hidden\" name=\"action\" value=\"error\">
<table>
<tr><td>Site:</td><td><input type=\"text\" name=\"site\"></td></tr>
<tr><td>Type of Error:</td>
<td><select name=\"error\">
<option value=\"1\">Hardware</option>
<option value=\"2\">Software</option></select></td></tr>
<textarea name="answer" rows="20" cols="70"></textarea>
<tr><td colspan=\"2\"><input type=\"submit\" value=\"Submit Error\"></td></tr>
</table>
</form>
</body>
</html>
}
}
if ($hard) {
print "Go to room 1";
}
if ($soft) {
print "Go to room 2";
}

Related

Perl - How to validate Chinese character input from web form?

My web page uses Charset UTF-8 to allow Chinese character input in a textarea form field. I want to test if the input contains a certain character. I've writtena test script to see how Perl is going to handle the Chinese input. It's not finding the match when there is a known match.
Here is my test form:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
</head>
<body>
<form method="post" action="http://www.my_domain.com/cgi-bin/my_test_script.pl">
<textarea name="user_input" rows="" cols=""></textarea>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Here is my code:
#!/usr/bin/perl -T
use strict;
use warnings;
use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use utf8;
print "Content-type: text/html; charset=UTF-8\n\n";
print "<meta http-equiv='content-type' content='text/html;charset=UTF-8'>";
my $query = new CGI;
my $msg = $query->param('user_input');
chomp $msg;
my $msg_code = ord($msg);
print "<p> Message was: ".$msg."\n";
print "<p> Message Code is: ".$msg_code."\n";
my $char_from_code_point = "\N{U+89C6}";
my $char_from_code_point_reverse_code = ord($char_from_code_point);
print "<p> char_from_code_point= ".$char_from_code_point."\n";
print "<p> char_from_code_point_reverse_code = ".$char_from_code_point_reverse_code."\n";
if ($msg =~ m/$char_from_code_point/) {
print "<p>Matched!\n";
}
else {
print "<p> NOT matched\n";
}
And here is the output from submitting the correct character:
Message was: 视
Message Code is: 232
char_from_code_point= 视
char_from_code_point_reverse_code = 35270
NOT matched
Could someone please point out what I'm doing wrong?
Thank you.

Why am I getting this error when testing a Perl/CGI scipt on Padre?

Spent better part of 3-days writing a CGI script to handle the input form data from my HTML. Used Padre as an editor but now receive this error when running the script.
"uncaught exception from user code: "-T" is on the #! line, it must also be used on the command line at scipt.pl"
I'd like some pointers if anyone is willing to look over my code and offer guidance. This is my first endeavor into Perl and CGI. What I desire for endstate is a form a webuser enters data and then hits submit. After validation a page is sent back to the browser with information and errors if they exist. here is my code and thanks in advance!
HTML Code:
<html>
<head><title>My First CGI-PERL</title>
<!--Link to css for styling here-->
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
<h1>Welcome to my first CGI-PERL Form submission</h1></header>
<br>
<p>Please Enter the following information in the fields and click
SUBMIT.</p>
<hr>
<br>
<div class="container1">
<form action="/cgi-bin/text.pl" method="post"> <!--script to process
this section-->
Item Number<input type="text" name="Item"><br> <!--Cannot be blank-->
Product Name<input type="text" name="Name"><br> <!--Cannot be
blank-->
Product Cost<input type="text" name="Cost"><br> <!--must be between .50 and $1000-->
Selling Price<input type="text" name="Price"><br> <!--price from 1.00 to 2000.00-->
Quantity on Hand<input type="text" name="Quantity"><br> <!--cannot be negative-->
</form></div>
<br>
<br>
<hr>
<br>
<h2>Choose A Product Category</h2> <!--must be one of the categories-->
<br>
<div class="container2">
<form action="/cgi-bin/radio.pl" method="post"> <!--name of script that handles this section-->
<input type="radio" name="letter" value="F">F<br>
<input type="radio" name="letter" value="H">H<br>
<input type="radio" name="letter" value="M">M<br>
<input type="radio" name="letter" value="C">C<br>
<input type="radio" name="letter" value="T">T<br>
<br>
</form></div>
<hr>
<br>
<div class="container4">
<form action="/cgi-bin/myfirstcgi.cgi" method="post"> <!--script to process submit and send a second page back-->
<input type="submit" name="submit" value="SUBMIT"><br>
</form></div> <!--close container 2-->
<!--Profit should be auto generated on the second page created by the script-->
</body>
</html>
Perl Script:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use strict;
use warnings;
use CGI qw(:standard);
print "<html><body>";
print "Thank you for submitting the form. <br>\n";
#return to html page with form
print "To go back to the form page click"; #how can i insert a link to the form page here
print "You chose item number ", param("Item")," \n";
print "The product name is ", param("Name"), " \n";
print "The Cost is ", param("Cost")," \n";
print "Selling Price ", param("Price")," \n";
print "Quantity on Hand is ", param("Quantity")," \n";
#scalar variables to hold form input data
my $item = param("Item");
my $name = param("Name");
my $cost = param("Cost");
my $price = param("Price");
my $category = param("Category"); #radio button chosen
my $quantity = param("Quantity");
my $profit = $cost - $price;
#radio buttons
my %categories = ("F", "H", "M", "C", "T");
#validation a category was chosen
my $categories = param("letter");
if (exists $categories{$category}) {
print "The product Category is $category";
}
else {
error ("You must select a category");
}
#validate input
if ($item eq "") {
error ("Field cannot be blank");
}
if ($name eq "") {
error ("Field cannot be blank");
}
if ($cost < .50 && $cost > 1000) {
error ("Invalid Entry Cost must be between $.50 and $1000");
}
if ($price < 1.00 && $cost > 2000) {
error ("Invalid Amount Please enter Price between $1.00 and $2000");
}
if ($quantity < 0) {
error ("Quantity cannot be negative number");
}
sub error {
my ($errormsg) = #_;
print "<h2>Error</h2>\n";
print "$errormsg<p>\n";
print "</body></html>\n";
exit;
}
Your homework should be done by you. But Since there is lot of syntax mistakes there I will try to help you.
First of all Bind all the elements inside the html into a single form with single submit button so that it can go to server in the form of query string in a single go.
Your html should be:
<html>
<head><title>My First CGI-PERL</title>
<!--Link to css for styling here-->
<!--<link rel="stylesheet" type="text/css" href="style.css">-->
</head>
<body>
<header>
<h1>Welcome to my first CGI-PERL Form submission</h1></header>
<br>
<p>Please Enter the following information in the fields and click
SUBMIT.</p>
<hr>
<br>
<div class="container1">
<form action="action.cgi" method="post"> <!--script to process
this section-->
Item Number<input type="text" name="Item"><br> <!--Cannot be blank-->
Product Name<input type="text" name="Name"><br> <!--Cannot be
blank-->
Product Cost<input type="text" name="Cost"><br> <!--must be between .50 and $1000-->
Selling Price<input type="text" name="Price"><br> <!--price from 1.00 to 2000.00-->
Quantity on Hand<input type="text" name="Quantity"><br> <!--cannot be negative-->
<br>
<br>
<hr>
<br>
<h2>Choose A Product Category</h2> <!--must be one of the categories-->
<br>
<div class="container2">
<input type="radio" name="letter" value="F">F<br>
<input type="radio" name="letter" value="H">H<br>
<input type="radio" name="letter" value="M">M<br>
<input type="radio" name="letter" value="C">C<br>
<input type="radio" name="letter" value="T">T<br>
<br>
<hr>
<br>
<div class="container4">
<input type="submit" name="submit" value="SUBMIT"><br>
</form></div> <!--close container 2-->
<!--Profit should be auto generated on the second page created by the script-->
</body>
</html>
Your cgi script I named as action.cgi and It should be like this for the above html from your approach. Whatever the error you had I tried to show it using commented lines.
#!/usr/bin/perl -T
print "Content-type: text/html\n\n";
use strict;
use warnings;
use CGI qw(:standard);
print "<html><body>";
print '<h1>"Thank you for submitting the form. <br>"\n';
#return to html page with form
print '<h2>To go back to the form page click Here.</h2>';#missing quotes
print "<hr><br><br>";
print "You chose item number ",param("Item")," <br>";
print "The product name is ", param("Name"), " <br>";print "The Cost is
", param("Cost")," \n";
print "Selling Price ", param("Price")," <br />";
print "Quantity on Hand is ", param("Quantity")," <br> ";
#scalar variables to hold form input data
my $item = param("Item");
my $name = param("Name");
my $cost = param("Cost");
my $price = param("Price");
my $category = param("Category"); #radio button chosen
my $quantity = param("Quantity");
my $profit = $cost - $price;
#radio buttons
my #categories = ("F", "H", "M", "C", "T");#better use array
#vali`dation a category was chosen
$category = param("letter");
if(grep { /$category/ } #categories) { # check if category exist in your predefined array
print "The product Category is $category \n";
}
else {
error ("You must select a category");
}
#validate input
if ($item eq "") {
error ("Field cannot be blank");
}
if ($name eq "" ){
error ("Field cannot be blank");
}
if ($cost < .50 && $cost > 1000) {
error ("Invalid Entry Cost must be between $.50 and $1000");
}
if ($price < 1.00 && $cost > 2000) {
error ("Invalid Amount Please enter Price between $1.00 and $2000");
}
if ($quantity < 0) {
error ("Quantity cannot be negative number");
}
#Error subroutine
sub error {
my $errormsg = shift;#you shouldn't assign array to variable
print "<h2>Error</h2>\n";
print "$errormsg<p>\n";
print "</body></html>\n";
}
If you really want to learn perl after basic understanding try to learn
perldsc,perlvar,perlref,perloo,perlobj
Padre runs Perl programs using a command like:
/usr/bin/perl <your_file.pl>
Taint checking requires deep changes to how the compiler works, so it needs to be turned on immediately after the compiler starts up. You have -T on the shebang line inside your program and that isn't parsed until after the compiler starts - too late for taint mode to be enabled. It would be confusing for Perl to start running your code not in taint mode when you think that taint mode has been turned on, so it halts execution with the error message that you have seen.
You can fix this by configuring Padre to run your code with as slightly different command:
/usr/bin/perl -T <your_file.pl>
In Padre choose Tools -> Preferences from the menu and then select the "Language - Perl 5" option. There is a text input labelled "Interpreter arguments". You can put your -T there and save the changes.
Also, I'll just reiterate my previous advice that using CGI in 2015 is ridiculous. Please take a look at CGI::Alternatives and switch to a more modern architecture.

html calling perl subroutine and return values from subroutine to display in the html

here is my html code
<html>
<title>Results</title>
<body><h1> Here are your results</h1>
<p>Please click the Button to see your result run by Ravi's team.</p>
<form action='index.pl' method='post'>
<input type='submit' value='submit'>
</form>
</body>
</html>
and index.pl is my perl and my subroutine is as follows.
sub my_result{
my $run;
my $dir="/kbio/sraja/BenzoExposedDataSet/database/Output";
my $parsebphtml = "/parse_bphtml.pl";
my $olgacsvfile = "/database/Output/sample.csv";
my #bp=<$dir/*.bp>;
$run ="perl $parsebphtml > $olgacsvfile";
# print "$com\n";
system($run)==0 or my_err("Could not run $run\n");
#printing the table
open(F,"$olgacsvfile") or my_err("Could not open the csv ($olgacsvfile) file");
print "<h2> Average Results </h2>";
print "<table border=1>";
while(my $line=<F>){
print "<tr>";
my #cells= split ',',$line;
foreach my $cell (#cells)
{
print "<td colspan=1>$cell</td>";
}
print "</tr>";
}
print "</table>";
}
So as you see, table is what i need to return to results.html
Any help would be really appreciable.
thanks .
Geet
I don't know how much work you want to do but, if you want to keep it simple give a try at the HTML::Template module. Here is a simple usage example.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>A random page</title>
</head>
<body>
<TMPL_VAR NAME=page_content>
</body>
</html>
My perl code contained something like this. Better yet, check the documentation at http://metacpan.org/pod/HTML::Template .
use HTML::Template;
sub my_result {
return $html_string;
}
my $master_template = HTML::Template->new(filename => "Path to html template file");
$master_template->param('page_content' => my_result());
Depending on how far you plan on going with this, I would recommend that you a more advanced templating system such as the one used by the mojolicious framework (http://mojolicio.us/perldoc/Mojo/Template).
Cheers,
MrMcKizzle

Web::Scrape with Xpath returns too many lines

Using Web::Scrape on some nasty nested tables, with no CSS style. Having to learn XPATH, and getting tripped up.
Updated: Fixed some XPATH issues, now just have one remaining question regarding attributes
#!perl
use warnings;
use Web::Scraper;
use Data::Dumper;
my $html = do { local $/; <DATA> };
my $scraper = scraper {
# Wrong! The 'tbody' element does not exist.
# process ".//[#id='cfg-surface-detail']/center/table/tbody/tr/td[2]/select",
# I used Chrome to get the XPath, and it inserts tbody elements when rendering bad HTML
# also, I changed the start of the XPATH from './/' to '//*'
# which I think means "relative to anywhere" or something.
process "//*[#id='cfg-surface-detail']/center/table/tr/td[2]/select",
'sensorType[]' => 'TEXT';
};
my $res = $scraper->scrape($html);
print Dumper($res);
__DATA__
<html><head><title>...</title></head>
<body>
<form action="/foo" method=post id=cfg-surface-detail name=cfg-surface-detail>
<center>
<table bgcolor="#FFFFFF">
<tr><td>Sensor Type</td><td>
<select name="cfg-sensor-type" >
<option value="1 Fred's Sensor" selected>Fred's Sensor
<option value="2 Other">Other Sensor
</select>
</td></tr>
</table>
</center>
</form>
</body>
</html>
This now outputs:
$VAR1 = {
'sensorType' => [
'Fred\'s Sensor Other Sensor '
]
};
So I'm getting close. How now do I specify the <option> that has the selected attribute?
Update: Solved. Xpath is //*[#id="cfg-surface-detail"]/center/table/tr/td[2]/select/option[#selected]
This helped: http://www.w3schools.com/xpath/xpath_syntax.asp
#!perl
use warnings;
use Web::Scraper;
use Data::Dumper;
my $html = do { local $/; <DATA> };
my $scraper = scraper {
process '#cfg-surface-detail//select',
'sensorType[]' => 'TEXT';
};
my $res = $scraper->scrape($html);
print Dumper($res);
__DATA__
<html><head><title>...</title></head>
<body>
<form action="/foo" method=post id=cfg-surface-detail name=cfg-surface-detail>
<center>
<table bgcolor="#FFFFFF">
<tr><td>Sensor Type</td><td>
<select name="cfg-sensor-type" >
<option value="1 Fred's Sensor" selected>Fred's Sensor
<option value="2 Other">Other Sensor
</select>
</td></tr>
</table>
</center>
</form>
</body>
</html>
If it were me, I'd go with css. Css solution for selected option is:
'select[name="cfg-sensor-type"] option[selected]'
The answer was a bit from both of the previous answers:
$scraper = scraper {
process '//select[#name="cfg-sensor-type"]/option[#selected]', 'SensorType' => 'TEXT';
};

Perl Form Validation using CGI scripting

I'm trying to achieve one last task for my assignment is to validate the form before submit it to the another CGI program.
What happen is that I have a simple CGI program that will ask user to input the data
#!/usr/bin/perl -w
use CGI qw/:standard/;
# Standard HTTP header
print header();
# Write information to data file and produce a form
&printForm();
# Finish HTML page
print end_html();
# This sub will create a form to access the print_fortune.cgi script
sub printForm
{
print qq~
<html>
<head><title>My Search Engine</title>
</head>
<body>
<form action="b1.cgi" method="GET">
What is your e-msil address? <input type="text" name="passing" size=40>
<input type="submit" value="send address">
<input type="hidden" name="form" value="insert" />
</form>
<form method="get" action="b1.cgi" enctype="application/x-www-form-urlencoded">
<input type="text" name="search" value="" size="30" /><br />
<label><input type="radio" name="option" value="name" checked="checked" />name</label>
<label><input type="radio" name="option" value="author" />author</label><label>
<input type="radio" name="option" value="url" />url</label>
<label><input type="radio" name="option" value="keyword" />keyword</label>
<input type="submit" name=".submit" value="Search" />
<input type="hidden" name="passing" value="http://default.com" />
<div><input type="hidden" name="form" value="search" /></div></form>
</body>
So the above program contains two forms. One is to add new data to the database and the other one is to search from the database.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use LWP::Simple;
use CGI;
use HTML::HeadParser;
use DBI;
my $serverName = "";
my $serverPort = "";
my $serverUser = "";
my $serverPass = "";
my $serverDb = "";
my $serverTabl = "";
$cgi = CGI->new;
my $pass = $cgi->param('passing');
$URL = get ("$pass");
$head = HTML::HeadParser->new;
$head->parse("$URL");
my $methods = $cgi->param('form');
if ($methods eq "insert"){
insert_entry();
}
show_entries();
sub insert_entry {
my ($dbh, $success, $name, $author, $url,$temp);
$dbh = DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;port=$serverPort",$serverUser,$serverPass);
$name = $head->header('X-Meta-Name');
$author = $head->header('X-Meta-Author');
$url = $cgi->param('passing');
$temp = $head->header('X-Meta-Keywords');
#keyword = split(/,/,$temp);
$success = $dbh->do("INSERT INTO $serverTabl(name,author,url,keyword1,keyword2,keyword3,keyword4,keyword5) VALUES(?,?,?,?,?,?,?,?)", undef,$name,$
author,$url,$keyword[0],$keyword[1],$keyword[2],$keyword[3],$keyword[4]);
$dbh->disconnect;
if($success != 1) {
return "Sorry, the database was unable to add your entry.
Please try again later.";
} else {
return;
}
}
sub show_entries {
my ($dbh, $sth, #row);
my $search = $cgi->param('search');
my $option = $cgi->param('option');
$dbh = DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;port=$serverPort",$serverUser,$serverPass);
$sth = $dbh->prepare("SELECT *
FROM $serverTabl
WHERE $option LIKE '%$search%'");
$sth->execute;
print "Existing Entries",HR;
while(#row = $sth->fetchrow_array) {
$row[5] = scalar(localtime($row[5]));
print "<table border='2'><tr>";
print "<td>" . $row[0] . "</td>";
print "<td>Name" . $row[1] . "</td>";
print "<td>Author" . $row[2] . "</td>";
print "<td>URL" . $row[3] . "</td>";
print "<td>Keyword1" . $row[4] . "</td>";
print "<td>Keyword2" . $row[5] . "</td>";
print "<td>Keyword3" . $row[6] . "</td>";
print "<td>Keyword4" . $row[7] . "</td>";
print "<td>Keyword5" . $row[8] . "</td>";
print "</tr></table>";
}
$sth->finish;
$dbh->disconnect;
}
So now the question is how can I do a regular expression for the form submission before it goes to the second program?
I want to do validation for
name allows spaces but only alphabetical characters
author allows spaces but only alphabetical characters
keywords allows no spaces and only alphabetical characters
url only allows alphanumerical characters and the following :/.~?=+& No two periods can exist consecutively.
I'm really sorry but I'm really new to Perl. We are only been taught about PHP, but Perl almost nothing....
The perluniprops Perl document lists all the \p regular expression properties.
For a string that contains only letters, you want
/^[\p{Alpha}]+$/
For a string that contains only letters and spaces you want
/^[\p{Alpha}\x20]+$/
To match a URL the documentation of the URI module gives this as an official pattern to match a URL
m|^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?$|
Be sure to cite the references in your work to get extra marks!