I have a javascript code that calls a function of a PHP api to insert a row in my database.
var url = 'http://myapi/OfertasVentaOnline/api.php?method=update&function=categoria&id='+id+'&idCategoria='+id_categoria+'&descripcion='+descripcion+'&format=json';
$.getJSON(url, function (data){
});
a sample of this url:
"http://myapi/OfertasVentaOnline/api.php?method=create&function=categoria&id=9999&idCategoria=3&descripcion=test%new%category&format=json"
in the url i replace the spaces for % in the descripcion parameter, as shown here the url is created correctly.
then I got my api who gets the call and decodes it. in the function that i use to decode and get the parameters:
$descripcion_cool = str_replace("%"," ",$_GET['descripcion']);
echo utf8_decode($descripcion_cool);
$query = 'INSERT INTO `Categorias`(`idCamping`, `idCategoria`, `descripcion`) VALUES (
"'.mysql_real_escape_string($_GET['id']).'",
"'.mysql_real_escape_string($_GET['idCategoria']).'",
"'.mysql_real_escape_string($descripcion_cool).'")';
this outputs this:
test new?tegoryINSERT INTO `Categorias`(`idCamping`, `idCategoria`, `descripcion`) VALUES (
"9999",
"3",
"test new�tegory")null
as you can see, php add some special characters to the url parameters. What am i doing wrogn ? thanks
PD: in my php file i tried to add:
header('Content-type: text/plain; charset=utf-8');
but it keeps doing the same.
Thanks in advance
Assuming you’re sending param values having spaces, you are encoding it by replace space with
%. Actually, you don’t have to do that. You can useencodeURIComponent()method if you want to encode params/quesrystings being passed over. And if you want to encode url, then useencodeURI()This will safely enocode url.