Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

inserting HTML formatting

Quote Reply
inserting HTML formatting
In a previous post on how to allow HTML formatting into the description field, Alex said:

"....Formatting will be preserved, however it won't insert HTML for you. So if a user enters in text with newlines, when displayed in HTML the newlines won't be shown. You'll need to do a s/\n/<BR>/g; in perl to covernt newlines to html <BR> tags."

My knowdledge of PERL is still at the "knowing just enough to be dangerous" level so could someone provide a more detailed explaination such as which file to update (add.cgi?) and where/how in the script to insert the s/\n/<BR>/g ?

Thanks in advance!
Quote Reply
Re: inserting HTML formatting In reply to
Try:

# Validate the form input..
$rec = &cgi_to_hash ( $in );
$in->{Description} =~ s/\n/<BR>/g;
$id = $val->add_record ( $rec );
if ($id) {

Looks like it should work. You can't apply reg exp to $in->param() like that. You need to do (it's ugly):

$val = $in->param('Description');
$val =~ s/\n/<BR>/g;
$in->param('Description', $val);

Hope this helps,

Alex
Quote Reply
Re: inserting HTML formatting In reply to
Never hurts to experiment...its one way of learning.
In add.cgi Before:
# Validate the form input..
$rec = &cgi_to_hash ( $in );

Try adding this line and see if it works.
$in->param('Description') =~ s/\n/<BR>/g;

Chris
Quote Reply
Re: inserting HTML formatting In reply to
No, It didn't like that at all. This is what I had:

# Validate the form input..
$in->param('Description') =~ s/\n/<BR>/g;
$rec = &cgi_to_hash ( $in );
$id = $val->add_record ( $rec );
if ($id) {

The add.cgi doesn't execute completely, and I get this software error:
Execution of /public_html/cgi/guide/add.cgi aborted due to compilation errors.

Quote Reply
Re: inserting HTML formatting In reply to
The flip side of this is that users can enter HTML tags into the descriptions -- you can note that on your add/modify form.

If you manually validate each entry, then it's not much of a problem.

We don't serve off-site banners for a lot of reasons -- meaning we screen out <IMG> tags. Up front I say it's speed -- and that's 90% true. The other is that the adult sites buy up domains that go dead and change all the links. We have a family site, and I'm not up to taking the chance that the postcards site suddenly starts serving up adult banners --- I can't do much about the URL itself, but that is not _on_ my site, if you see the difference.

It's something to consider in allowing HTML -- that people can insert -- and change -- banners or images that are being linked to.

I've successfully used pretty complex formatting -- including nested <HTML></HTML> tags without a problem in MSIE and the current Netscape.

I use <P><BR><B><A><UL><LI> and other tags in almost every description.

[This message has been edited by pugdog (edited November 09, 1999).]
Quote Reply
Re: inserting HTML formatting In reply to
No, it didn't work. this is what I added:

# Validate the form input..
$rec = &cgi_to_hash ( $in );
$in->{Description} =~ s/\n/<BR>/g;
$id = $val->add_record ( $rec );
if ($id) {

The form processed but did not keep any of the page breaks. Ideas??
Quote Reply
Re: inserting HTML formatting In reply to
I received the email (validation) confirmation for each of the tests I did.

The emails did contain the correct formatting. So I don't know if that's because the email did something or perhaps when the Build All had an affect.
Quote Reply
Re: inserting HTML formatting In reply to
Ooops, what was I thinking. You don't want:

$in->{Description} =~ s/\n/<BR>/g;

But:

$rec->{Description} =~ s/\n/<BR>/g;

However I think you may be confused. All you are doing here is replacing newlins with <br> tags. You do this because in HTML, newlines are not displayed. Extra whitespace is just ignored. So the record may look weird when you view it through email.

Cheers,

Alex
Quote Reply
Re: inserting HTML formatting In reply to
That's it exactly!

Thanks again for your help.
Ralph
Quote Reply
Re: inserting HTML formatting In reply to
I am confused by this thread. What do you do to get \n to be substituted to a <BR> tag?

I am trying to do it in admin.cgi under the sub add record so that when I add an article, the newlines are preserved when adding it from admin.

I have tried

$in->{Description} =~ s/\n/<BR>/g;

in
sub add_record {
# --------------------------------------------------------
# Adds a record to the database.
#
my ($in, $db) = @_;

# Fix up the category name.
($in->param('CategoryID') !~ /^\d+$/) and $in->param(-name => 'CategoryID', -value => &get_category_id($in->param('CategoryID')));

$in->{Article} =~ s/\n/<BR>/g;

my $id = $db->add_record (&cgi_to_hash($in), $in);
$id ? &html_add_success ($in, $db, $id) : &html_add_failure ($in, $db, $Links: BSQL::error);
}

Of course I get software error.

Thanks, Kevin
Quote Reply
Re: inserting HTML formatting In reply to
I figured it out. You just need to add

my $val = $in->param('Article');
$val =~ s/\n/<BR>/g;
$in->param('Article', $val);

$in->{Article} =~ s/\n/<BR>/g;


How would you allow all fields to do this??

Do you have to do a foreach ??
Quote Reply
Re: inserting HTML formatting In reply to
I would do this only for displaying, not when saving the data. You should really save the data without html formatting, and then format it for display. This makes it easier when you need to put it into a form field, or perhaps email the data.

You could do all the fields like:

foreach ($in->param) {
$val = $in->param($_);
$val =~ s/\n/<br>/g;
$in->param($_, $val);
}

assuming you wanted to do it for a CGI object. If it's just in a hash:

foreach (keys %in) {
$in{$_} =~ s/\n/<br>/g;
}

Hope that helps,

Alex
Quote Reply
Re: [rholmin] inserting HTML formatting In reply to
Hi, this is exactly what I need to finish the preparation to launch my LinksSQL (2 not 1) site. I just want to have the linebreaks converted to <br> in the display of the Detailed and Description firlds so that I do not have to insert linebreaks in all the database.

I am a bit confused by the code posted here. Could someone please clarify? I think this is an important thing to know, I expect that anyone using detailed pages will want to format the text output.

Any help is appreciated.

cdkrg

Able2Know :: Ajooja Directory

Last edited by:

cdkrg: Oct 5, 2003, 9:54 PM
Quote Reply
Re: [cdkrg] inserting HTML formatting In reply to
I posted a global in the thread that you originally posted. It should do what you want.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] inserting HTML formatting In reply to
Sorry bout that Andy. I'd found one and deleted my help requests.

cdkrg

Able2Know :: Ajooja Directory