Hello,

I am currently trying to develope a perl CGI script that will allow a user to enter a name on an HTML form, and it will search the database with that name. Once it finds the matched names, it will display their name and telephone number on the screen. I'm not completely sure how to go about this, so I was wondering if anyone could share any sort of insight.

Thank you very much in advance.

HTML FORM

<HTML>
<HEAD><TITLE>Search Telephone Number</TITLE></HEAD>
<BODY BGCOLOR=#FFFFCC>
<H3>Search A Telephone Number</H3>
<FORM ACTION="project2_searchnum.pl" METHOD=POST>
<TABLE>
<TR>
    <TD>Name:</TD>
    <TD><INPUT NAME=fullname SIZE="20"></TD>
</TR>
</TABLE>
<P><INPUT TYPE=submit VALUE="Submit"> <INPUT TYPE=reset> <INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1);return true;"></P>
</FORM>

</BODY>
</HTML>

PERL SCRIPT

#!/usr/bin/perl

#use warnings;
#use strict;

use DBI;
use DBD::mysql;
use CGI qw( :standard );

my $cgi = new CGI;

print "Content-type: text/html\n\n";

#connecting to the database 
my $dbh = DBI->connect( 
"DBI:mysql:****", "******", "******" ) or
   die( "Could not make connection to database: $DBI::errstr" );

#setting params from the html form
$q=new CGI;
$name=$q->param('fullname');
$number=$q->param('number');



#Webpage creation
print "<HTML>\n";
print "<HEAD><TITLE>Project 1 - Search a Telephone Number</TITLE><BASEFONT SIZE=5></HEAD>\n";
print "<H1>Search a Telephone Number</H1>\n";
print "<BODY BGCOLOR=#FFFFCC>\n";

#Display names and numbers from search here

print "<a href='project2_main.html'><INPUT TYPE='button' VALUE='Main Menu'</a>\n";

print "</BODY>\n";
print "</HTML>\n";

$sth->finish();
$dbh->disconnect();

If you've named your table phonelist and your columns name and number, insert this at line 23:

my $sth = $dbh->prepare("SELECT name,number from phonelist where name=?");
$sth->execute($name);
my @row;

At line 33, insert this:

while (@row = $sth->fetchrow_array)
  {print "Found:  Name=$row[0] Phone Number=$row[1]<br>\n";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.