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!

How to output plain text?

Status
Not open for further replies.

gnubie

Programmer
Apr 16, 2002
103
US
I have a single Perl script with a dispatcher which I want to display either an HTML FORM or the plain text results.

FORM displays properly. Clicking Submit results in a download dialog box, instead of just displaying the plain text results (in a browser window). Using print "Content-type: text/plain\n\n"; doesn't work. How can I just display plain text (which contains HTML) but with NO HTML interpretation?

Code:
#!/usr/bin/perl
#
# Test text/plain
#

use CGI qw/:standard/;
use strict;   # we need every bit of help we can get

# Dispatcher
# Get operation
my $operation = param('oP'); # DISPLAYLISTING or DISPLAYFORM

if ($operation eq "") {
  $operation = "DISPLAYFORM"; # default
}
if ($operation eq "DISPLAYFORM") {
  displayForm();
} elsif ($operation eq "DISPLAYLISTING") {
  displayListing();
} else {
  die "INTERNAL ERROR: $operation is an unknown operation";
}
exit(0);

sub displayForm {
  my $size=80;
  print "Content-type: text/html\n\n";
  print <<EOM;
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
	<title>Display Form</title>
</head>

<h1 align=&quot;center&quot;>Listing Generator</h1>

<table border=0>
<form action=&quot;&quot; method=&quot;POST&quot;>
  <tr>
    <td> </td><td><input name=&quot;oP&quot; type=&quot;hidden&quot; value=&quot;DISPLAYLISTING&quot;></td>
  </tr>
  <tr>
    <td>Title</td><td><input name=&quot;title&quot; type=&quot;text&quot; value=&quot;Jar&quot; size=80></td>
  </tr>
    <tr>
    <td colspan=2><input type=&quot;submit&quot; value=&quot;Submit&quot;></td>
  </tr>
</form>
</table>
</body>
</html>
EOM
}

sub displayListing {
  print &quot;Content-type: text/plain\n\n&quot;;
  my $title=param('title');
  print &quot;<h1>Title: $title</h1>\n&quot;;
}
1

 
G,

What webserver, OS etc, coz some are more forgiving than others

--Paul
 
Local PC web server is Xitami, remote server is Apache. Both exibit the same symptoms. OS=XP; Browser=IE6.
 
Try using the encoded characters in your print statements:

Instead of < use & lt;
Instead of > use & gt;
Instead of &quot; use & quot;

IMPORTANT: There must be NO space between &quot;&&quot; and the rest of the code (lt; gt; and qout;), I placed space here because it doesn't display otherwise.



 
Yes, I could re-code the output, but I was hoping for simpler solution. Any idea why
Code:
print &quot;Content-type text/plain/n/n&quot;;
at the beginning of the output doesn't work?
 
Hmm, what if you print the output in a text file and then make a redirect to it?

open(TXTFILE, &quot;>temporary.txt&quot;);
print TXTFILE &quot;<h1>Title</h1>\n&quot;;
etc ... print all the code
close(TXTFILE);

print &quot;Location:

(You'll probably need to write to temporary.txt outside the cgi-bin in order to display it then)
 
Code:
print &quot;Content-type text/plain/n/n&quot;;

Should be

print &quot;Content-type text/plain\n\n&quot;;

HTH
--Paul

 
Code:
print &quot;Content-type text/plain/n/n&quot;;

Should be

print &quot;Content-type: text/plain\n\n&quot;;

HTH
--Paul

 
If you expect to view it through a browser, you're going to have to have at least &quot;some&quot; HTML coding. You're browser has a really tough time without it.

There's always a better way. The fun is trying to find it!
 
Update:
1. print &quot;location: ...&quot; didn't work. It just displayed the web page.
2. Content-type text/plain didn't work. It just printed and was apparently ignored.
3. I ended up writing the output to a file, reading it back in and substituting the character entities for the angle brackets as c4n suggested, and print to screen.

Thanks for your help.

Dick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top