2

I can't figure out why my php processing script stops when it encounters a special character in a tinymce textarea. example if I type foo and submit, fine...no problems but if I type foo<<<, it stops after foo when I submit the editor is creating the html entities and sending them through ajax

getting the content with

var c = tinyMCE.get('content').getContent();

and sending the content

ajax.send("action=edit_content&c="+c+"&id="+id);

and I can see in firebug that the string is being passed

action=edit_content&c=<p>foo &lt;&lt;&lt;</p>&id=8

and the php is really nothing special at all, just set that post to a var

is it maybe because of the & in the &lt; ? maybe it thinks that is actually another post parameter?

I am still getting my feet wet when it comes to ajax. If I am correct on my assumption, how do I fix that?

Fred Turner
  • 139
  • 1
  • 9
  • Yes, possibly. Show `QUERY_STRING` and `$_GET`. Utilize POST instead, or [url encoding](http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript). – mario Jul 07 '13 at 13:30

1 Answers1

3

You have the right idea. The ampersand is breaking the URL string.

In order to fix breaking characters, you have to escape the string.

Try this:

ajax.send("action=edit_content&c="+escape(c)+"&id="+id);

You probably won't have to (because Apache will do it for you), but if necessary, you can also unescape the string on the PHP side using urldecode:

<?php echo urldecode($_GET['c']); ?>
Steven Moseley
  • 15,871
  • 4
  • 39
  • 50
  • awesome !! now that works, but I see that it also adds quite a bit to the markup. would I be better off to maybe set up a regex before I send it so it finds all those and replace them with something else then have php replace that back again after processing and before putting it into the database – Fred Turner Jul 07 '13 at 13:38
  • No. That's what `escape` does - it replaces special characters with pre-defined url-safe strings. Don't reinvent the wheel. Use the system that everyone else does. – Steven Moseley Jul 07 '13 at 13:47
  • 1
    look at this question: http://stackoverflow.com/questions/6986742/problem-with-sign-when-using-ajax-to-post-data-to-php for another possible solution, escape() in my case transform "too much" the string. – Matteo Bononi 'peorthyr' Apr 22 '15 at 10:52