$_GET doesn’t catch "+"

Asked

Viewed 99 times

0

I am recovering a value from an AJAX request, it works fine but it has a problem. The "+" character is replaced by a blank one

Dell printer (supporting eSF 2.1+)

Stays

Dell printer (supporting eSF 2.1 )

And this is giving "bomb" in the system

 var XMLHttp =  generateXMLHttp();
    XMLHttp.open("get", "classes/getDataPropCom.php?qtd_embarcados=" + qtdUsuarios +"&id=" + id, true);
    XMLHttp.onreadystatechange = function () {

        if (XMLHttp.readyState == 4){
            if (XMLHttp.status == 200) {
            }
        }
});

jquery takes the value with the right "+" Now the GET rescue from Ajax does not take the "+"

When he gets here.... doesn’t take the "+"

if(isset($_GET['qtd_embarcados']) && isset($_GET['id'])){
    $qtd_embarcados = $_GET['qtd_embarcados'];
    $id = $_GET['id'];

    if($qtd_embarcados != ''){
        if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
            /* special ajax here */
            $PropostaComercial->getValue($qtd_embarcados,$id);
        }else if(empty($usuarios)){
            $PropostaComercial->getValue($qtd_embarcados,$id);
        }
        else{
            $PropostaComercial->getValue($qtd_embarcados,$id);
        }
    }
}

how I take the values

 if($("#ComercialEDEP10 h5").html() != undefined){
        id = encodeURIComponent($("#ComercialEDEP10 h5").html());
    }

    if($("#EducacionalEDEP10 h5").html() != undefined){
        id = encodeURIComponent($("#EducacionalEDEP10 h5").html());
    }
    if($("#ProfissionalEDEP10 h5").html() != undefined){
        id = encodeURIComponent($("#ProfissionalEDEP10 h5").html());
    }
  • That text with the + comes from the server or goes to the server?

  • It is a database value, which I display on the screen, once displayed on the screen, I take the contents of it, once taken sending via ajax to run on an sql

  • But then the problem is when you get it from the server or when you send it back?

  • Like, I transfer via ajax... in this ajax gets nice, then when php with $_GET[] will get the value that gives the error

1 answer

2


You have to handle that string before sending to ajax.

var string = 'Dell printer (supporting eSF 2.1+)';
var stringTratada = encodeURIComponent(string);

console.log(stringTratada);

So no characters are lost and it is in a standard format.

And in PHP if necessary (because $_GET already does it) you can use urldecode to convert back to the readable string.

  • Only in php there is no way ne?

  • 1

    @gabrielfalieri in browser o + is there, right? then you have to send it to the server and characters like white space and some symbols like + are not allowed without being converted. Hence the encodeURIComponent

  • 1

    yes yes, that’s right

  • 1

    Why .html() and not .text()? And what gives console.log("classes/getDataPropCom.php?qtd_embarcados=" + qtdUsuarios +"&id=" + id);?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.