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?
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.
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:
jaleman:
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…
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:
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:
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
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.