How to handle special characters in HTML?

Using Servoy to administrate the content of your website? Discuss all webrelated Servoy topics on this forum!

How to handle special characters in HTML?

Postby swingman » Sun Sep 26, 2004 2:36 pm

Hi, I have the following setup:

Servoy 2.1 with a mySQL database holding text for web pages.
PHP to create XHTML webpages.

The users type text into fields in Servoy, and can see a rough preview (in an HTMLarea) of what the page will look like. It all works fine until they type quotes, &, accented characters etc. The Servoy HTML preview gets confused displaying the characters as square boxes. The same happens in the webpage created with PHP.

I tried using for quotes. Neither Servoy or PHP likes it, displaying the and instead of the quotes...

What is the correct way of encoding the characters?
- using Javascript in Servoy?
- using PHP?
User avatar
swingman
 
Posts: 1472
Joined: Wed Oct 01, 2003 10:20 am
Location: London

Postby Jan Blok » Thu Sep 30, 2004 12:18 pm

replace special chars wtih html code,like:
'<' - "& l t ;" (unable to post without spaces)
'>' - "& g t ;" (unable to post without spaces)
etc.
Jan Blok
Servoy
Jan Blok
 
Posts: 2684
Joined: Mon Jun 23, 2003 11:15 am
Location: Amsterdam

Postby swingman » Thu Sep 30, 2004 1:09 pm

Hi Jan,

I was hoping there was some Servoy built-in function I hadn't spotted to deal with this...

I will need to write a function which translates all special characters... I will start writing somthing based this code I just found on

http://developer.irt.org/script/243.htm
User avatar
swingman
 
Posts: 1472
Joined: Wed Oct 01, 2003 10:20 am
Location: London

Postby IT2Be » Thu Sep 30, 2004 3:22 pm

I would be able to include such a thing in my tools plugin. Let me know and I will do somewhere over the weekend...
Marcel J.G. Trapman (IT2BE)
SAN partner - Freelance Java and Servoy
Servoy Components - IT2BE Plug-ins and Beans for Servoy
ServoyForge - Open Source Components for Servoy
User avatar
IT2Be
Servoy Expert
 
Posts: 4766
Joined: Tue Oct 14, 2003 7:09 pm
Location: Germany

Postby Jan Aleman » Thu Sep 30, 2004 8:55 pm

try:

Code: Select all
encodeURIComponent(htmlcode)


eg:
Code: Select all
var htmlcode = '<>'
var encodedHtml = encodeURIComponent(htmlcode)


note that this encodes to UTF-8 escape sequences. It should work in all browsers though.
Jan Aleman
Servoy
Jan Aleman
 
Posts: 2083
Joined: Wed Apr 23, 2003 9:49 pm
Location: Planet Earth

Postby swingman » Fri Oct 01, 2004 9:14 am

jaleman wrote:try:

Code: Select all
encodeURIComponent(htmlcode)


eg:
Code: Select all
var htmlcode = '<>'
var encodedHtml = encodeURIComponent(htmlcode)


note that this encodes to UTF-8 escape sequences. It should work in all browsers though.


I tried, this but the HTMLArea just displays these codes raw! Lots of %20s instead of spaces etc...

To reiterate:

The problem I'm trying to solve is as follows:

1. User types text into normal text area.
2. There is a calculated field which adds basic CSS styling to the text. The result is displayed in an HTML area.
3. The text is stored in an mySQL database which will be accessed by PHP to build XHTML pages.

I'm sure anyone who tries to manage a website content from Servoy will come across the very same problem.

Marcel, I like your idea of writing it into your plug-in... :-)

Here are a couple of screenshots to give you an idea:
Attachments
storyencoded.gif
The encoding confuses the display even more!
storyencoded.gif (56.3 KiB) Viewed 9758 times
storynoencoding.gif
No encoding applied. See how the quotes are handled, and how they affect the display of the link.
storynoencoding.gif (23.87 KiB) Viewed 9757 times
User avatar
swingman
 
Posts: 1472
Joined: Wed Oct 01, 2003 10:20 am
Location: London

Postby Jan Aleman » Fri Oct 01, 2004 2:15 pm

I see in your examples that you are using high ascii characters. Is your backend database set to the correct character set? If you use Sybase by default it uses UTF encoding which supports any characterset. I think default mysql databases don't.
Jan Aleman
Servoy
Jan Aleman
 
Posts: 2083
Joined: Wed Apr 23, 2003 9:49 pm
Location: Planet Earth

Postby automazione » Fri Oct 01, 2004 3:38 pm

Hi swingman,

Probably mine is not a stylish solution, but I had a similar problem and I solved it replacing the "strange high ASCII" characters like or ΄ with plain low ASCII " or ' before storing the text area field:

    htmltext_field = htmltext_field.replace('', '"')
    htmltext_field = htmltext_field.replace('΄', "'")

etc....

hope it helps...
Enrico Arata
Servoy Italia
automazione
 
Posts: 366
Joined: Thu Apr 24, 2003 11:37 am
Location: Torino, Italy

Postby swingman » Fri Oct 01, 2004 4:05 pm

jaleman wrote:I see in your examples that you are using high ascii characters. Is your backend database set to the correct character set? If you use Sybase by default it uses UTF encoding which supports any characterset. I think default mysql databases don't.


Didn't think of that! I will have a look at my mySQL database....
User avatar
swingman
 
Posts: 1472
Joined: Wed Oct 01, 2003 10:20 am
Location: London

Postby swingman » Thu Oct 07, 2004 12:40 am

Hi Jan,

You are right! The version of mySQL at my webhost is prior to 4.1, so it does not support UTF-8, so it simply strips out all our high ascii characters.

I have come up with the following work-around:

- OnRecordSave I encode all text fields with the following global function, called encodeText:

Code: Select all
var s = arguments[0];
var i = 0;
while (i < s.length)
{
   var ccode = s.charCodeAt(i);
   if(ccode >= 160) {
      var entity = '&#' + ccode + ';';
      s = utils.stringReplace(s,s[i],entity);
      i += entity.length;
   } else {
      i += 1;
   }
}

return s;


OnRecordEdit I decode, using a global function called decodeText, so my user can see what they have typed:

Code: Select all
var s = arguments[0];
var startIndex = s.indexOf('&#',0);
while (startIndex > -1)
{
   var endIndex = s.indexOf(';',startIndex);
   if(endIndex > -1) {
      var entity = s.substring(startIndex,endIndex + 1);
      s = utils.stringReplace(s,entity,String.fromCharCode(utils.stringToNumber(entity)));
   }   
   startIndex = s.indexOf('&#',0);
}
return s;


My next question: Is there any way of looping through all editable text fields on a form?
:-)

Otherwise I will have to write OnRecordEdit and OnRecordSave handlers for every form :-(
User avatar
swingman
 
Posts: 1472
Joined: Wed Oct 01, 2003 10:20 am
Location: London

Postby Jan Aleman » Thu Oct 07, 2004 8:21 am

probably the easiest is to adjust your method to read the name of the element that triggered it using Application.getMethodTriggeredElementName.
Name the elements that need to have validation before you use this.

Alternatively:
Put some identifiable in the elementname that tells you it must be validated, then in your onrecordsave and edit loop through all elements and validate the ones that contain your specific criteria for validation.
Jan Aleman
Servoy
Jan Aleman
 
Posts: 2083
Joined: Wed Apr 23, 2003 9:49 pm
Location: Planet Earth

Postby swingman » Thu Oct 07, 2004 10:51 am

One small Problem:
My encode function encodes the old value of the last field edited...

How do I ensure I get the actual field contents?
User avatar
swingman
 
Posts: 1472
Joined: Wed Oct 01, 2003 10:20 am
Location: London

Postby swingman » Thu Oct 07, 2004 11:25 am

Looks like adding

application.updateUI();

before I encode the fields sort this out.
User avatar
swingman
 
Posts: 1472
Joined: Wed Oct 01, 2003 10:20 am
Location: London


Return to Web Development

Who is online

Users browsing this forum: No registered users and 9 guests

cron