HTML form is not passing paramters to PERL CGI script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newtoperl101
    New Member
    • Jul 2013
    • 1

    HTML form is not passing paramters to PERL CGI script

    Hi,
    I am using two files sqlconfig.cgi and contactform.htm l, the former being the script that links to the html. However, when I run the html file from the browser it simply publishes the script instead of passing parameters.
    Yes, I have configured Apache for CGI and gave write permissions to my files.
    here are the codes:
    1. sqlconfig.cgi
    Code:
    #!C:\Dwimperl\perl\bin\perl.exe
    # PERL MODULES WE WILL BE USING
    use CGI;
    use DBI;
    use DBD::mysql;
    
    # Config DB variables
    our $platform = "mysql";
    our $database = "test";
    our $host = "localhost";
    our $port = "3306";
    our $tablename = "addressbook";
    our $user = "root";
    our $pw = "password";
    our $q = new CGI;
    
    # DATA SOURCE NAME
    $dsn = "dbi:mysql:$database:localhost:3306"; 
    
    # PERL DBI CONNECT
    $connect = DBI->connect($dsn, $user, $pw);
    
    #Get the parameter from your html form.
    $lname=$q->param('lname');
    $fname=$q->param('fname');
    $phone=$q->param('phone');
    $email=$q->param('email');
    $address=$q->param('address');
    $zip=$q->param('zip');
    
    print $q->header; 
    print "Hello $fname $lname";
    $sql="INSERT INTO test.addressbook(last_name,first_name) values('$lname','$fname')";
    $sth = $connect->prepare($sql)
    or die "Can't prepare $sql: $connect->errstrn";
    #pass sql query to database handle..
    
    $rv = $sth->execute
    or die "can't execute the query: $sth->errstrn";
    #execute your query
    
    if ($rv==1){
    print "Record has been successfully updated !!!";
    }else{
    print "Error!!while inserting recordn";
    exit;
    }
    2. contactsform.ht ml
    Code:
    <html>
    <head>
      <title>ADDRESS BOOK</title>
    </head> 
    <body bgcolor="#FFFFFF" link="#0000FF" alink="#FF0000" vlink="#C000FF">
      <h1>ADDRESS BOOK CONTACTS</h1> 
      <table> 
      <form method="get" action="C:/Apache/Apache2/cgi-bin/sqlconfig.cgi"> 
        <tr> 
          <td align="right">Last Name:</td> 
          <td align="left"><input type="text" name="lname" size="15" maxlength="50"></td> 
          <td align="right">First Name:</td> 
          <td align="left"><input type="text" name="fname" size="15" maxlength="50"></td> 
        </tr> 
        <tr> 
          <td align="right">Phone:</td> 
          <td align="left"><input type="text" name="phone" size="15" maxlength="50"></td> 
          <td align="right">Email:</td> 
          <td align="left"><input type="text" name="email" size="15" maxlength="50"></td> 
        </tr> 
        <tr> 
          <td align="right">Address:</td> 
          <td align="left"><input type="text" name="address" size="15" maxlength="50"></td> 
          <td align="right">Zip Code:</td> 
          <td align="left"><input type="text" name="zip" size="15" maxlength="50"></td> 
        </tr> 
      </table> 
      <input type="submit" value="Submit" onclick="addcontact">
      </form> 
    </body> </html>
    Can someone please check where is the error.
    Last edited by Frinavale; Jul 4 '13, 01:26 PM. Reason: Fixed code tags and indented HTML for readability purposes
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Your HTML is invalid.
    You start your <form> inside an open <table> tag; however, this should be above the open <table> tag.

    -Frinny

    Comment

    • RonB
      Recognized Expert Contributor
      • Jun 2009
      • 589

      #3
      The opening and closing form tags could be either inside or outside of the opening/closing table tags, but not mixed one inside and the other outside.

      You may also need to change the path used in the action attribute. It should either be a full url, or a path based from the web server's document root. Using a system path will work in some cases, but not all; it depends on the web server being used.

      The perl script "should" work, but could use some improvements.

      Comment

      Working...