I have one problem with function in Javascript. When I insert text in textarea, function sends text to PHP script with AJAX, but I have problem when I insert two or three words, e.g Bosnia and Herzegovina then the script does not work. I used string replace :
function provjeraDrzave(rijec) {
rijec = rijec.replace(" ", "%20");
$.ajax({
type: "GET",
url: "/drzava.php?slovo=" + randomslovo + "&drzava=" + rijec,
success: function (odgovor) {
$('#rezultati').replaceWith($("<span id='rezultati'>" + odgovor + "</span>"));
},
error: function () {
alert('Doslo je do pogreske');
}
});
}
It should work as follows: When I insert Bosnia and Herzegovina that must change to Bosnia%20and%20Herzegovina but that change to Bosnia%20and Herzegovina and that does not work. Where is the problem ??
Why don’t you use, for example, the native
encodeURIComponentfunction, which is made for this?Or, even better, let jQuery take care of URL encoding for you, using the
dataconfig param: