B
Bart Van der Donck
Jomo said:That's great. Thanks.
Now what if I want to display the results within a
specific section of an existing HTML page?
Not a problem. You could make a fixed string inside your HTML file (eg
inside a comment tag). Then read the file and replace that string by
your XML output result, before showing in to screen.
Say this is your HTML template, stored as /path/to/your/file.html:
<html>
<body>
<hr>My header<hr>
<!--g58s5s26d58esskolKjdX-->
<hr>My footer<hr>
</body>
</html>
Your CGI file could then look like this:
#!/usr/bin/perl
print "Content-Type: text/html\n\n";
use CGI::Carp qw(fatalsToBrowser);
use XML::Simple;
my $found=0;
my $q=$ENV{"QUERY_STRING"};
$q=~s/^artist=//;
$q=~tr/+/ /;
$q=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
my $xmlfile = XMLin("data.xml", forcearray=>1);
for my $record (@{$xmlfile->{RECORD}})
{
if (uc $record->{"ARTIST"}->[0] eq uc $q)
{
$result.=$record->{"URL"}->[0];
$found=1;
}
}
if ($found!=1)
{
print "<html><body>
No website found
</body></html>";
}
else
{
open (R, "/path/to/your/file.html") || die "$!";
flock(R, 1) || die "Cant get LOCK_SH on file: $!";
while (<R>)
{
$_=~s/<!--g58s5s26d58esskolKjdX-->/$result/i;
print $_;
}
close R;
}