Jump to content

ScArides

New Members
  • Posts

    1
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

ScArides's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. For every poor soul trying to find a solution like i did and finding this forum: I have a html form. Users write something in it (name, company name, ..) The form gets sent to a php page which writes the values into a database. All special chars get screwed up into trash. My solution: After going crazy with all the encodings, i wrote my own encoding. Here is a sample of what you have to do (it's not that hard): 1) You write a javaScript that activates when you click the submit button. This script will then go through all the form fields and replace all the special chars with something like #01 #02 etc. Then it will send the form. So if you have a text 'kůň', it can translate into something like 'k#11#15' 2) Your php script that accepts that form must decode this back - then you can write it into the database and be fine. I found out that most of the problems are caused by the html forms, not the scripts or databases themselves. This will work around that problem. Sample code: JavaScript: function encode() { var result = ''; var temp = document.getElementById(myFormTextField).value; //Read value of the TextBox for (var k=0; k<temp.length; k++) { //Go through the value one character at a time var currentChar = temp.substring(k,k+1); if (currentChar == 'š') { //Replace all special characters with our coding sequence result += '#01'; } else if (currentChar == 'Š') { result += '#02'; } else if (currentChar == 'č') { result += '#03'; } else if (currentChar == 'Č') { result += '#04'; } else { //Leave standard characters as they are result += currentChar; } } document.getElementById(myFormTextField).value = result; //Write the encoded text back into the TextBox } Decoding (written in java, but you'll get the idea and re-write it to php) public String decode(String source) { String result = ""; for (int i=0; i< source.length(); i++) { String piece = source.substring(i,i+1); if (piece.equals("#")) { //If we detect '#', we take a chunk of 3 characters String chunk = source.substring(i,i+3); // and decode the chunk i+=2; if (chunk.equals("#01")) { result += "š"; } else if (chunk.equals("#02")) { result += "Š"; } else if (chunk.equals("#03")) { result += "č"; } else if (chunk.equals("#04")) { result += "Č"; } } else { //If there is no '#' char, we simply read 1 character result += piece; } } return result; }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.