views:

14

answers:

2

I'm using a form to record data in my database using AJAX. So, how this works? I get the form data via Javascript, request a URL which is written in PHP then insert to the database.

The problem is that I'm using Latin 1 characters so the form is expecting also accented characters like ó. I need to convert this ó to html (ó) in javascript, so that I can pass it to PHP.

How doI do that?

+1  A: 

This should do it for you:

function escapeHTMLEncode(str) {
  var div = document.createElement(‘div’);
  var text = document.createTextNode(str);
  div.appendChild(text);
  return div.innerHTML;
 }

http://sanzon.wordpress.com/2008/05/01/neat-little-html-encoding-trick-in-javascript/

Tom Gullen
+1  A: 

I think the best way to do it is to make an AJAX request to a PHP page outputting the result of htmlentities().

kbok