I have a web app that accepts and saves plain text input. The relevant JS looks like
var newText = document.getElementById("textarea").value;
newText = encodeURIComponent(newText);
var save = new XMLHttpRequest();
save.open('POST', 'write.php', true);
save.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
save.send('u=' + user + '&text=' + newText);
and the relevant PHP looks like
$savetext = $_POST['text'];
$savetext = rawurldecode($savetext);
At this point, if a user originally typed %20 it is saved as a space.
I also tried replacing %20 with + in the JS after encodeURIComponent
and before sending it, but it didn't seem to help. I still want to preserve actual spaces of course, but in case someone needs to write the actual three character string %20, I don't want it to disappear.