1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Passing values to multiple scripts using hidden fields.

Discussion in 'Software Development' started by Anarchist, Nov 21, 2003.

Thread Status:
Not open for further replies.
Advertisement
  1. Anarchist

    Anarchist Thread Starter

    Joined:
    Nov 21, 2003
    Messages:
    2
    Hello,
    I am trying to write a script that validates user input on a form then passes the data entered to a second script for processing.

    My problem is that only one of the items chosen is being sent for processing, whatever comes after that item is ignored.

    I would appreciate any help on this as I have tried everything trying to get all of the items processed.

    Here is my ailing code:


    First Script:
    Code:
    #!perl
     use cgi;
    #parse the form arguments into variables to hold data
    
    
    
    
    #variables to hold data if no data entered output message
    if ( !($name=CGI::param("name")) )
    {
            #output error page if no data entered
            &outputErr("Please enter your name - Hit the
            back button and enter the missing information");
    }
    
    if ( !($address=CGI::param("address")) )
    {
            #output error page if no data entered
            &outputErr("Please enter your address - Hit the
            back button and enter the missing information");
    }
    
    if ( !($items=CGI::param("items")) )
    {
            #output error page if no data entered
            &outputErr("Please select an item - Hit the
            back button and enter the missing information");
    }
    
    
    
    #output web page
    print <<PAGE ;
    Content-type: text/html \n\n
    <html>
    <head>
    <title>SUCCESS</title>
    </head>
    <body>
    <font color="white"><font size="+5"><font face="vladimir script">
    Do you want to proceed with this purchase?</font>
     <br>
    
     <br>
    
    
    <body bgcolor="#000000" >
    <font color="white"><font size=+1><i>
    Name:$name <br>
    <p>
    Address:  $address <br>
    <p>
    Purchase{s}: $items<br>
    
    
    
    <br>
    <br>
    
    <FORM ACTION="http://crass.serveftp.com/cgi-bin/ica2.cgi" method=post>
     <INPUT TYPE="HIDDEN" NAME="name" value="$name">
    
    <INPUT TYPE="HIDDEN" NAME="address"  VALUE="$address">
     <INPUT TYPE="HIDDEN" NAME="items" value="$items">
    <INPUT TYPE="submit" VALUE="SUBMIT" >
    </form>
    
    
    
    
    
    
    PAGE
    
    #end the script
    exit 0;
    
    sub outputErr
    {
    #output error message
    print <<ERRPAGE ;
    Content-type: text/html\n\n
    <html>
    <head>
    <title>Error</title>
    </head>
    @_[0]
    </body></html>\n
    ERRPAGE
    
    #end script processing
    exit 0
    }
    
    Second Script:

    Code:
    #!perl
    #Declare the path to the perl interpreter
    
    #include the library for parsing input
    use CGI;
    
    
    
    #parse the form arguments into variables to hold data
    $name=CGI::param("name");
    $address=CGI::param("address");
    @items=CGI::param("items");
    
     $cost=0;
    
    #Test whether name entered matches the special
    #name if the name matches free poster else no poster
    
    {
            if ($name =~ m/henry/)
            {
                    $result = "yes";
                    $match = 1;
            }
    }
    
    if ($match == 0)
    {
            $result = "no";
    }
    
    
    
    #Open the file containing the cost data and hold the items in an array
    open(FILE, "prices1.txt") || &outputErr("File access error");;
    @list=<FILE>;
    close FILE;
    
    
    #initialise a hash with the item prices from the array
    %price=("autographs",$list[0],"doll",$list[1],"shoes",$list[2],"cards",$list[3],"jacket",$list[4],"sunglasses",$list[5],"jumpsuit",$list[6]);
    print "Content-type: text/html\r\n\r\n";#remove this line later
    #determine the cost of the items
    foreach $item (@items)
    {
    print "\$item = $item<br>";
    $Cost+=$price{$item};
    }
    
    
    
    
    
    #format the price output
    $cost = sprintf("%.2f",$cost);
    
    #open file for appending or print error page if access denied
    open(OUT,">>orders.txt") || &outputErr("File Access Denied");
    #lock access
    flock(OUT,2);
    #write the entered order data to the file
    print OUT "name,$name \n";
    print OUT "address, $address \n";
    print OUT "item, @items \n";
    print OUT "cost, $Cost \n";
    print OUT "poster,$result \n";
    print OUT "********************************************************* \n";
    #unlock access
    flock(OUT,8);
    #close the file
    close(OUT);
    
    
    
    
    
    
    #output web page
    print <<PAGE ;
    Content-type: text/html \n\n
    <html>
    <head>
    <title>SUCCESS</title>
    </head>
    <body>
    <font color="white"><font size="+5"><font face="vladimir script">
    Good Choice</font>
     <br>
     <br>
    
    
    <body bgcolor="#000000" >
    <font color="white"><font size=+1><i>
    Name:$name <br>
    <p>
    Address:  $address <br>
    <p>
    Purchase{s}: @items<br>
    <p>
    FREE POSTER? = $result
    
    <font size=+3><br><font size=3>Your choice costs £$Cost</font></br>
    <br>
    <br>
    
    
    
    
    
    <center><font size="+5"><font face="vladimir script">Your order has been processed<br>
    Thankyou for shopping with us.</center></font></font>
    
    </body></html></i></font><br>
    
    
    PAGE
    
    #end the script
    exit 0;
    
    
    
    ###############################################start sub##################
    sub outputErr
    {
    #output error message
    print <<ERRPAGE ;
    Content-type: text/html\n\n
    <html>
    <head>
    <title>Error</title>
    </head>
    @_[0]
    </body></html>\n
    ERRPAGE
    
    #end script processing
    exit 0
    }
    
    Cheers Anarchist
     
  2. io_robert

    io_robert

    Joined:
    Sep 7, 2003
    Messages:
    27
    Instead of using multiple IF statements, try using a case. Im not familiar with Perl but most programming languages employ the Case. Just some other suggestions in yor IF statements try this logic:

    IF variableName = Null then
    Output "Error"
    Else
    VariableName = UserInputValue

    * To me it seems that although you have an error method to catch a user not entering a value it doesnt seem to store a value if one has been entered. To debug this try hardwiring values in the code and check if it displays on the form and has passed on to the other script.
     
  3. Anarchist

    Anarchist Thread Starter

    Joined:
    Nov 21, 2003
    Messages:
    2
    Hi,
    I have tried using a loop for this but no joy, it still wont print my chosen items completely.

    If I change a value in the code to @items I can get all of the selected items to print BUT I lose my calculated cost print out.


    Code:
    #!perl
    #Declare the path to the perl interpreter
    
    #include the library for parsing input
    use CGI;
    
    
    
    #parse the form arguments into variables to hold data
    $name=CGI::param("name");
    $address=CGI::param("address");
    @items=CGI::param("items");
    
     $cost=0;
    
    #Test whether name entered matches the special
    #name if the name matches free poster else no poster
    
    {
            if ($name =~ m/freddy/)
            {
                    $result = "yes";
                    $match = 1;
            }
    }
    
    if ($match == 0)
    {
            $result = "no";
    }
    
    
    
    #Open the file containing the cost data and hold the items in an array
    open(FILE, "prices1.txt") || &outputErr("File access error");;
    @list=<FILE>;
    close FILE;
    
    
    #initialise a hash with the item prices from the array
    %price=("autographs",$list[0],"doll",$list[1],"shoes",$list[2],"cards",$list[3],"jacket",$list[4],"sunglasses",$list[5],"jumpsuit",$list[6]);
    #determine the cost of the items
    foreach $item (@items)
    {
            $Cost+=$price{$item};
    }
    
    
    
    
    
    #format the price output
    $cost = sprintf("%.2f",$cost);
    
    #open file for appending or print error page if access denied
    open(OUT,">>orders.txt") || &outputErr("File Access Denied");
    #lock access
    flock(OUT,2);
    #write the entered order data to the file
    print OUT "name,$name \n";
    print OUT "address, $address \n";
    print OUT "item, @items \n";
    print OUT "cost, $Cost \n";
    print OUT "poster,$result \n";
    print OUT "********************************************************* \n";
    #unlock access
    flock(OUT,8);
    #close the file
    close(OUT);
    
    
    
    
    
    
    #output web page
    print <<PAGE ;
    Content-type: text/html \n\n
    <html>
    <head>
    <title>SUCCESS</title>
    </head>
    <body>
    <font color="white"><font size="+5"><font face="vladimir script">
    Good Choice</font>
     <br>
     <br>
    
    
    <body bgcolor="#000000" >
    <font color="white"><font size=+1><i>
    Name:$name <br>
    <p>
    Address:  $address <br>
    <p>
    Purchase{s}: @items<br>
    <p>
    FREE POSTER? = $result
    
    <font size=+3><br><font size=3>Your choice costs £$Cost</font></br>
    <br>
    <br>
    
    
    
    
    
    <center><font size="+5"><font face="vladimir script">Your order has been processed<br>
    Thankyou for shopping with us.</center></font></font>
    
    </body></html></i></font><br>
    
    
    PAGE
    
    #end the script
    exit 0;
    
    
    
    ###############################################start sub##################
    sub outputErr
    {
    #output error message
    print <<ERRPAGE ;
    Content-type: text/html\n\n
    <html>
    <head>
    <title>Error</title>
    </head>
    @_[0]
    </body></html>\n
    ERRPAGE
    
    #end script processing
    exit 0
    }
    
    Cheers Anarchist
     
  4. Lori713

    Lori713

    Joined:
    Sep 8, 2003
    Messages:
    50
    Hi and Welcome,

    I ran into your exact problem while building some pages. I would be happy to send you the scripts and templates I used, since it would take a lot of explanation and posting of code to make it clear...

    [On the other hand, you can always use the recursive method of calling the page itself, which would then keep the variables (but since I needed to launch a new window, that didn't work for me).]

    The short of it is that I use Perl, CGI.pm and HTML::Template to pass stuff around to various scripts when they're called. I concatenate all my variable values with a unique divider (like a "~") and stuff that into a .tmpl that's used as a TMPL_INCLUDE.

    Once the next script is called, I use CGI to snag that passed hidden variable (in the TMPL_INCLUDE), split it out (based on the "~" character, and assign it to my variables as needed. A little klunky but functional.

    By the way, I have the whole process of passing these often-used (and often constant) variables in a package so I don't have to keep re-typing and re-declaring the variables in every subsequent .pl.

    I've typed this response about ten times, and it's still confusing. Send me email at perlgeek7 "at" yahoo "dot" com if you'd like to see any of this stuff. I'll be happy to explain what I've figured out so far. But fair warning: I'm still relatively new to this; I just recently got this passing of variables stuff figured out in a way that was acceptable to me and my deadlines... ;-)
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/181300