I'm fairly new at writing perl scripts and was wondering if you can write a perl script on an html page for the submission of a form to a database.
Any assistance would be greatly appreciated.
Thank you
Doug
I'm fairly new at writing perl scripts and was wondering if you can write a perl script on an html page for the submission of a form to a database.
Any assistance would be greatly appreciated.
Thank you
Doug
Perl can absolutely receive a form and write to a database. asked the right question; and pointed toward deployment choices. For learning, a single script that parses the form and uses DBI to insert records is the fastest route. For anything beyond a toy project, consider PSGI/Plack or a small framework (Dancer2 or Mojolicious) so you get routing, sessions and easier testing.
A minimal example showing form parameter handling, basic validation and a safe DB insert (use placeholders to avoid SQL injection):
#!/usr/bin/env perl
use strict;
use warnings;
use CGI::Simple;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
my $q = CGI::Simple->new;
my $name = $q->param('name') || '';
my $email = $q->param('email') || '';
$name =~ s/^\s+|\s+$//g;
die "Invalid email" unless $email =~ /\S+\@\S+/;
my $dbh = DBI->connect("DBI:mysql:dbname=app_db;host=localhost", "dbuser", "dbpass",
{ RaiseError => 1, AutoCommit => 1 });
my $sth = $dbh->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
$sth->execute($name, $email);
print "Content-Type: text/html; charset=UTF-8\n\n";
print "<p>Thanks — saved.</p>";
$dbh->disconnect; Quick checklist and tips: set the correct shebang and executable bit (chmod 755); install DBI and the proper DBD driver (DBD::mysql, DBD::Pg, DBD::SQLite); validate and normalize every input; always use prepared statements (placeholders) rather than interpolating values; escape output to avoid XSS; use HTTPS and a CSRF token for forms that change state. For debugging enable CGI::Carp or check the server error log. For long-term maintainability and better performance look at PSGI/Plack (Plack/PSGI) and read the DBI docs for database usage (DBI docs). For form validation see Data::FormValidator; for full apps consider Dancer2 or Mojolicious.
Jump to Post— almostbob 866perl scripts in the /cgi-bin/ folder and just point the action of the form at the perl script
<form id='Globe' action='/cgi-bin/process.cgi' method='post'>Not even sure if perl scripts work outside of the cgi-bin folder, ?
perl scripts in the /cgi-bin/ folder and just point the action of the form at the perl script <form id='Globe' action='/cgi-bin/process.cgi' method='post'> Not even sure if perl scripts work outside of the cgi-bin folder, ?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.