LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-10-2005, 03:02 AM   #1
BroX
Member
 
Registered: Oct 2003
Location: Sweden
Distribution: Slackware64-current, SlackwareARM-15.0
Posts: 833

Rep: Reputation: 90
Line breaks in html with perl script


Hey all,

I have no clue about programming, however I manage (by error and trial mainly) to modify preexisting scripts. But this thing is killing me! I have the following code in a perl script that should write html form input to a file and return the same parameter/value pairs back to the internet page so the person can read what he/she submitted.

The data that's written to the file is nicely formatted, i.e. with line breaks. However, and here is my problem:

The parameter/value pairs that are returned to the page are all in one line. Whatever I try, with /n or <br>, it has no effect.

Here is part of the perl script. The bold part is the problem.
Code:
 print FILEHANDLE "--Submitted--: $Hour:$Minute $Day-$Month-$Year\n"; #write to file 
 
  print "<h2>Thank you for your registration!</h2><br>"; 
  print "<h3>The following details have successfully been submitted at $Hour:$Minute $Day-$Month-$Year:</h3><br>"; #writes to page 
  print ""; 
 
  foreach $parameter (@params) { 
 
        print FILEHANDLE "$parameter:" . param($parameter) ,"\n"; #writes to file 
 
        print "$parameter " . param($parameter) ,"<br>"; #writes to page 
  } 
 
  print FILEHANDLE "\n"; 
  close (FILEHANDLE); #close the file 
 
  print end_html;
This is what I now get returned to the page:
Code:
title Dr. firstname=Leon lastname=blabla institute=blabla department=blabla address1= address2= city= zip= country= tel= email= info=More blabla
Can somebody give me a hint here?

Cheers, Leon.
 
Old 01-10-2005, 03:09 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Re: Line breaks in html with perl script

Quote:
Originally posted by LJSBrokken
[B] print "$parameter " . param($parameter) ,"<br>"; #writes to page
Try:
Code:
        print "$parameter " . param($parameter) ."<br>"; #writes to page
i.e. replace the comma with a dot. With a comma, the <br> is passed as a second parameter to print. I'm not quite sure how Perl handles this, but you can find out with:
Code:
perldoc -f print
With a dot between two strings, Perl will concatenate the two strings and treat them as a single parameter to print.
 
Old 01-10-2005, 03:40 AM   #3
BroX
Member
 
Registered: Oct 2003
Location: Sweden
Distribution: Slackware64-current, SlackwareARM-15.0
Posts: 833

Original Poster
Rep: Reputation: 90
Quote:
i.e. replace the comma with a dot. With a comma, the <br> is passed as a second parameter to print.
Thanks for your reply, but unfortunately a dot instead of a comma gives exactly the same output...

Other options?

Cheers, Leon,
 
Old 01-10-2005, 05:02 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Just add a "\n" like :
Code:
print "$parameter " . param($parameter) ."<br>\n"; #writes to page
 
Old 01-10-2005, 05:13 AM   #5
BroX
Member
 
Registered: Oct 2003
Location: Sweden
Distribution: Slackware64-current, SlackwareARM-15.0
Posts: 833

Original Poster
Rep: Reputation: 90
Quote:
Originally posted by Cedrik
Just add a "\n" like :
Code:
print "$parameter " . param($parameter) ."<br>\n"; #writes to page
I tried that, but that doesn't work...
 
Old 01-10-2005, 05:18 AM   #6
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Please post the HTML code that your program produces (rather than the text as rendered by your browser).
 
Old 01-10-2005, 05:27 AM   #7
BroX
Member
 
Registered: Oct 2003
Location: Sweden
Distribution: Slackware64-current, SlackwareARM-15.0
Posts: 833

Original Poster
Rep: Reputation: 90
Hmmm... let me guess... must have something to do with XHTML instead of HTML? Anyways, here is the source code of the returned page:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
	PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"
	"http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"><head><title>Registration</title>
</head><body><h2>Thank you for your registration!</h2><h3>The following details have successfully been submitted at 13:25 10-1-2005:</h3>title Ms.
firstname=Leon
lastname=blabla
institute=blabla
department=blabla
address1=
address2=
city=
zip=
country=
tel=
email=
delegate=full
acc_person=no
vegetarian=no
info=
<br>
</body></html>
 
Old 01-10-2005, 05:35 AM   #8
Genjix
Member
 
Registered: Oct 2003
Location: Pico
Distribution: SUSE 9.1
Posts: 83

Rep: Reputation: 15
Quote:
The parameter/value pairs that are returned to the page are all in one line. Whatever I try, with /n or <br>, it has no effect.
im assuming your not stupid, but theyre all on one line because there are no line breaks between them.
 
Old 01-10-2005, 05:36 AM   #9
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
First off, with XHTML you should use <br/> not <br> (in XHTML, all tags must be closed; using a / at the end of the tag is a short-hand for spelling out the closing tag).

You also have no <p>…</p> tag enclosing the text in the body. You can't just type text, it needs to be enclosed in a block-level tag such as <p> or <hnumber>. You might like to look at http://validator.w3.org/

You do seem to be missing the <br> from the end of each line. I'm not certain what's happening to cause this problem; do you get this with a dot as well as with a comma? I would suggest trying it as two commands:
Code:
print "$parameter " . param($parameter); # writes to page
print "<br>\n"; #writes to page
 
Old 01-10-2005, 07:36 AM   #10
BroX
Member
 
Registered: Oct 2003
Location: Sweden
Distribution: Slackware64-current, SlackwareARM-15.0
Posts: 833

Original Poster
Rep: Reputation: 90
Genjix:
I know the line breaks are missing, my question was how to modify the perl script so they would be there.

Rjlee:
Why does the code get returned as XHTML and not HTML?

Writing it as two separate lines gives exactly the same result (source code also). Similar when I write <br/> instead of <br>. As judged from the html source, it appears that the <br> only gets added after the whole list of parameters (or am I wrong?), instead of after each of them.

Where would I add the <p>....</p> in the perl script?

Here's the complete perl script I use, maybe that will shed a light on the matter:
Code:
#!/usr/bin/perl  
 
use CGI qw(:standard); 
 
print header; 
print start_html('Registration'); 
 
if (param()) { 
   
  @params=param(); 
 
  ($Second,$Minute,$Hour,$Day,$Month,$Year,$WeekDay,$DayOfYear,$IsDST)=localtime(time()); 
 
  unless(open (FILEHANDLE, '>>/www/users/l/leobro/participants.txt')) #open file for appending 
  { 
        print "could not open file"; 
        print end_html; 
        exit(0); 
  } 
  $Month+=1; 
  $Year+=1900; 
 
  print FILEHANDLE "--Submitted--: $Hour:$Minute $Day-$Month-$Year\n"; #write to file 
 
  print "<h2>Thank you for your registration!</h2>"; 
  print "<h3>The following details have successfully been submitted at $Hour:$Minute $Day-$Month-$Year:</h3>"; #writes to page 
  print ""; 
 
  foreach $parameter (@params) { 
 
        print FILEHANDLE "$parameter:" . param($parameter) ,"\n"; #writes to file 
 
        print "$parameter " . param($parameter); 
        print "<br>\n"; #writes to page 
} 
 
  print FILEHANDLE "\n"; 
  close (FILEHANDLE); #close the file 
 
  print end_html; 
 
}else{ 
 
    print "You need to have a html form to pass parameters from"; 
    print end_html; 
}

Last edited by BroX; 01-10-2005 at 07:41 AM.
 
Old 01-10-2005, 07:44 AM   #11
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
He said you <br /> not <br/> (not the space between the r and the /)

try :
print "$parameter " . param($parameter) . "<br />\n";

(here the \n is not required but it is just for good formating xhtml code)
 
Old 01-10-2005, 07:56 AM   #12
BroX
Member
 
Registered: Oct 2003
Location: Sweden
Distribution: Slackware64-current, SlackwareARM-15.0
Posts: 833

Original Poster
Rep: Reputation: 90
Quote:
Originally posted by Cedrik
He said you <br /> not <br/> (not the space between the r and the /)

try :
print "$parameter " . param($parameter) . "<br />\n";

(here the \n is not required but it is just for good formating xhtml code)
Tried that <br /> as well, without result...
 
Old 01-10-2005, 08:06 AM   #13
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
What web browser do you use (with version number) ?
 
Old 01-10-2005, 08:14 AM   #14
BroX
Member
 
Registered: Oct 2003
Location: Sweden
Distribution: Slackware64-current, SlackwareARM-15.0
Posts: 833

Original Poster
Rep: Reputation: 90
Quote:
Originally posted by Cedrik
What web browser do you use (with version number) ?
Firefox 1.0 on Slack-current.
I will check how a w2k box behaves (with MSIExplorer Mozilla and Firefox).
 
Old 01-10-2005, 08:19 AM   #15
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
this works for me...

Code:
# debug, print params
# ===================

sub print_params
{
    if (param()) {
	
	print qq(<pre>);
	foreach $param (param()) {
	    print "$param = " . param($param) . "\n" ;
	}
	print qq(<pre>);
	print hr;
    }
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need REG EXP Help to remove line breaks webshark Programming 18 07-08-2005 04:31 PM
PERL script OK at command line, not in browser alvo Programming 4 12-19-2004 08:28 AM
Perl shell-out to script dunna work. Works on command line. Why? jlangelier Linux - Software 1 08-28-2004 02:00 AM
Perl Script Reading a txt file and generate html to be published! kofi Linux - Software 1 09-22-2003 05:12 PM
set content-type to 'text/html' in sendmail, using perl script brokenfeet Programming 3 08-05-2003 02:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration