Thread: for problem...

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    0
    Rep Power
    0
    I'm creating a simple phone directory program for my website, and it has an add numbers feature on it, which i can't get the last part to work.

    here is my code:
    ***************************
    #!/usr/local/bin/perl

    require ("cgi-lib.pl");
    &ReadParse(*form);

    print "Content-type:text/htmlnn";

    $cpass = "randels";
    $clogin = "ryan";

    if(($form{'action'} eq "searchforname") and ($form{'ulogin'} eq $clogin) and ($form{'upass'} eq $cpass)){
    &search;
    } else {
    if($form{'action'} eq "sp"){
    if(($form{'ulogin'} eq $clogin) and ($form{'upass'} eq $cpass)){
    &searchpage;
    } else {
    print "login incorrect!n";
    }
    } elsif($form{'action'} eq "addnumbers") {
    &addnumbers;
    } elsif($form{'action'} eq "addnumberstwo"){
    &addnumberstwo;
    } elsif($form{'action'} eq "addnumbersthree"){
    &addnumbersthree;
    } else {
    &login;
    }
    }

    sub login {
    print <<END_OF_LOG;
    <html>
    <body>
    <form method=post action="http://spazznt.hypermart.net/cgi-bin/phonedir.cgi">
    username:<input type="text" name="ulogin"><br>
    password:<input type="password" name="upass"><br>
    <input type="hidden" name="action" value="sp">
    <input type="submit" name="submit" value="login">
    </form><br><br><br>
    <form method=post action="http://spazznt.hypermart.net/cgi-bin/phonedir.cgi">
    <input type="hidden" name="action" value="addnumbers">
    <input type="submit" name="submit" value="addnumbers">
    </form>
    </body>
    </html>
    END_OF_LOG
    }

    sub searchpage {
    print <<END_OF_VGN;
    <html>
    <head><title>Spazznet's Conestoga Phone Directory</title></head>
    <body bgcolor="#FFFFFF">
    <form method="post" action="http://spazznt.hypermart.net/cgi-bin/phonedir.cgi">
    Enter Last name: <input type="text" name="searchterm" size="50"><br>
    <input type="hidden" name="ulogin" value="$clogin">
    <input type="hidden" name="upass" value="$cpass">
    <input type="hidden" name="action" value="searchforname">
    <input type="submit" name="submit" value="Search">
    </form></body></html>
    END_OF_VGN
    }


    sub search {
    open (phonedir, "phonedir.txt");
    @file = <phonedir>;
    close (phonedir);
    $name_found = 0;
    $alreadyprinted = 0;
    foreach $line (@file) {
    chomp $line;
    ($name,$grade,$phone) = split(/|/, $line);
    ($lastname,$firstname) = split(/./, $name);
    if($form{'searchterm'} =~ /^$lastname/i){
    if($alreadyprinted == 0){
    print <<END_OF_SEARCHRESULTS;
    <html>
    <head><title>Spazznet Directory Search Results</title></head>
    <body bgcolor="#FFFFFF">
    <table width="75%" border="1" cellspacing="0" cellpadding="3" align="center" bordercolorlight="#000000" bordercolordark="#000000">
    <tr bgcolor="#FF6600">
    <td width="25%">
    <div align="center"><font color="#FFFFFF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">Last
    Name</font></b></font></div>
    </td>
    <td width="26%">
    <div align="center"><font color="#FFFFFF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">First
    Name</font></b></font></div>
    </td>
    <td width="3%">
    <div align="center"><font color="#FFFFFF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">Grade</font></b></font></div>
    </td>
    <td width="46%">
    <div align="center"><font color="#FFFFFF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="-2">Phone
    Number</font></b></font></div>
    </td>
    </tr>
    END_OF_SEARCHRESULTS
    $alreadyprinted = 1;
    }
    print <<END_OF_RESULTS;
    <tr>
    <td width="25%">$lastname</td>
    <td width="25%">$firstname</td>
    <td width="5%">$grade</td>
    <td width="45%">$phone</td>
    </tr>
    END_OF_RESULTS
    $name_found = 1;
    }
    }
    if($name_found == 1){
    print <<END_OF_FOOTER;
    </table>
    </body></html>
    END_OF_FOOTER
    } else {
    print "Your searched name has not been found. Try again!";
    }
    }

    sub addnumbers {
    print <<END_OF_ADD;
    <html>
    <head><title>Add Numbers to the Spazznet Internet Directory</title></head>
    <body bgcolor="#FFFFFF">
    <form method=post action="http://spazznt.hypermart.net/cgi-bin/phonedir.cgi">
    number of numbers:<input type="text" name="numofnums">
    <input type="submit" name="submit" value="Submit">
    <input type="hidden" name="action" value="addnumberstwo">
    </form>
    </body>
    </html>
    END_OF_ADD
    }

    sub addnumberstwo {
    print <<END_OF_NUMTWO;
    <html>
    <head><title>Add Numbers to the Spazznet Internet Directory</title></head>
    <body bgcolor="#FFFFFF">
    <form method=post action="http://spazznt.hypermart.net/cgi-bin/phonedir.cgi">
    END_OF_NUMTWO
    for ($i = 1; $i <= $form{'numofnums'}; $i++) {
    print <<END_OF_NTT;
    Lastname:<input type="text" name="lastname$i"><br>
    First Name:<input type="text" name="firstname$i"><br>
    Grade:<input type="text" name="grade$i"><br>
    Phone #:<input type="text" name="phonenum$i"><br>
    END_OF_NTT
    }
    print <<END_OF_NTTH;
    <input type="hidden" name="action" value="addnumbersthree">
    <input type="hidden" name="numofnums" value="$form{'numofnums'}">
    <input type="submit" name="submit" value="submit">
    </form>
    </body></html>
    END_OF_NTTH
    }

    sub addnumbersthree {
    print <<END_OF_ADDT;
    <HTML>
    <HEAD><TITLE>...</TITLE></HEAD>
    <BODY BGCOLOR="#FFFFFF">
    END_OF_ADDT
    print "$form{'lastname1'}n";
    for ($p = 1; $p <= $form{'numofnums'}; $p++) {
    print "lastname$pn";
    print "$form{'lastname$p'}n";
    }
    print "</body></html>n";
    }
    *********************

    and the very last part, sub addnumbersthree doesn't work, it won't pull the information from the form before it. anybody have a reason why? and a simple explanation please.

    thanks


    ------------------
    inx
    http://spazznt.hypermart.net
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    82
    Rep Power
    14

    Ok, I have your final subroutine getting this data in the %form hash:

    numofnums = 1
    firstname1 = Josh
    phonenum1 = 123 4566
    submit = submit
    grade1 = 2
    action = addnumbersthree
    lastname1 = Schoof

    This is when I added one number. What are you not getting that you want?

    Josh
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    82
    Rep Power
    14
    Here:

    sub addnumbersthree {

    print <<END_OF_ADDT;
    <HTML>
    <HEAD><TITLE>...</TITLE></HEAD>
    <BODY BGCOLOR="#FFFFFF">
    END_OF_ADDT
    #print "$form{'lastname1'}n";
    for ($p = 1; $p <= $form{'numofnums'}; $p++) {
    print "lastname$pn";
    $key = 'lastname' . $p;
    print "$form{$key}";
    }
    print "</body></html>n";
    }

    Coupla things:

    It didnt interpolate your $p in $form{'lastname$p'} because of the single quotes. Thus, it was not retrieving any values, and also not printing them. My $key fix above is lame but you get the idea. Just watch how it stuffs the vars and you should be fine.

    Josh

    [This message has been edited by JSchoof (edited October 02, 2000).]

IMN logo majestic logo threadwatch logo seochat tools logo