tags:

views:

64

answers:

1

Okay, I'll admit being a designer, I don't know much about how data is handled from the servers in forms... (basically the whole programming part). Basically I have a text field that the user types something like "apple", this gets sent to the server and then displays the apple.html page that I have in a directory in an IFrame. If anyone can show me how to do this in a PHP script or Perl, would be great thanks! I think this is good start, it gets more complicated than this... I later have to convert the "apple" into something like 2321 and display 2321.html instead.

If anyone can find a CGI script that does this, thanks!

+1  A: 

Following is a complete working CGI program written in Perl. It does exactly what you required. Apart from your numbered fruit result documents, you also need a fall-back document sorry-no-such-fruit.html that is shown if the look-up did not succeed or the user did not input a fruit at all.

#!/usr/bin/perl -T
use strict;
use warnings;
use CGI ();

my %fruit_codes = (
    apple   => '2321.html',
    banana  => '1234.html',
    coconut => '8889.html',
);

my $c = CGI->new;
my $fruit_parameter = $c->param('fruit_name');
my $iframe_document;

if (defined $fruit_parameter and exists $fruit_codes{$fruit_parameter}) {
    $iframe_document = $fruit_codes{$fruit_parameter};
} else {
    $iframe_document = 'sorry-no-such-fruit.html';
}

$c->header('application/xhtml+xml');

print <<"END_OF_HTML";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
        <title>Fruits</title>
    </head>
    <body>
        <form action="fruits.cgi">
            <fieldset>
                <label for="fruit">Name of the fruit:</label>
                <input id="fruit" name="fruit_name" type="text" />
                <input type="submit" />
            </fieldset>
        </form>
        <iframe src="$iframe_document">
            <a href="$iframe_document">resulting fruit</a>
        </iframe>
    </body>
</html>
END_OF_HTML

1;

In case you want to modify this program, I give you the most important security advice: do not output user supplied input (in this case, $fruit_parameter) unmodified. This can be abused for at least cross-site scripting. Ask an expert programmer for help.

daxim
Thanks daxim, I've just noticed my host doesn't allow Perl unless I pay for it... can you suggest a way I can do this in PHP? Is there any way I can contact you about the code above?
Haskella
I don't do PHP.
daxim