Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

another cgi problem

Status
Not open for further replies.

ailse

Programmer
Jan 20, 2004
79
GB
Hi again,

I am in the process of converting a perl program to cgi so it can be run from a website instead of locally on my machine, and my question is about passing variables to the program - the code i have so far looks like this:

Code:
if (exists $HoA{$id}) {
     	print "$id has been located! Here are the associated values:\n";
	@id_array = split(/ /, $HoA{$id});
	
	foreach (@id_array) {
  	print "$_\n";
	}

	} 
	else {
      	print "This ID has not been found. Please enter another ID.\n"; 
   	}

so basically the user enters an ID and this is looked up as the key in a hash of arrays, if it is found the associated array is returned. the user types this ID into a form, and my problem is how do I pass this variable as $ID to the above code? I am using cgi.pm,

cheers for any advice :)
 
if you use GET then it will become appended to the URL - i.e. [red]?ID=12345[/red] - this is $ENV{'QUERY_STRING'}

If you use POST then it will be held in $ENV{'CONTENT_LENGTH'} - i.e.

Code:
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
print "$buffer\n\n";


Kind Regards
Duncan
 
cheers for that - here's the code i'm using now. It doesn't write the results out, should I be modifying it further?

Code:
my $id = param('id');

if (exists $HoA{$id}) {
         print "$id has been located! Here are the associated values:\n";
    @id_array = split(/ /, $HoA{$id});
    
    foreach (@id_array) {
      print "$_\n";
    }

    } 
    else {
          print "This ID has not been found. Please enter another ID.\n"; 
       }

where the value of 'id' has come from the user input to a form... is there something more I need for this to work?

thanks :)
 
are you certain values exist for $HoA{?}?

... or it could be a case mismatch:- ID / id


Kind Regards
Duncan
 
no, i checked that and to test the script I am only entering ID values that I know are in the HoA... I just wonder if there's any special syntax I need in the cgi program to print statements? I am just using the regular "print" but do I need something different since I am printing something that will be displayed in a browser?

sorry if that doesn't make much sense but I am very new to CGI.pm and trying to get my head round it!

cheers :)
 
have you chomped the user input to remove the CR/LF?


Kind Regards
Duncan
 
not too sure what you mean... here's the code I have so far:

Code:
$query = new CGI;

print $query -> header;

print $query -> start_html (-title => 'Your Results');

print $query -> h1({-align=>center}, $query->font({-color=>red}, 'Your Results'));


print "The associated array is:";

$id = param('id');

if (exists $HoA{$id}) {
     	print "$id has been located! Here are the associated values:\n";
	@array = split(/ /, $HoA{$id});
	
	foreach (@array) {
  	print "$_\n";
	}

	} 
	else {
      	print "This ID has not been found. Please enter another ID.\n"; 
   	}

and i'm getting the user input of the id from the html as follows:

Code:
<input type="text" name="id" size="30" maxlength="50">

if anyone can shed some light on this i'd be very grateful! i know there isn't a problem with creating the hash of arrays or looking up keys as i first made this as a regular perl prog and it works fine... just need it to work on the web!!

thanks :)
 
ok all sorted now! found my mistake!

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top