Pass variable via GET URL jQuery

Asked

Viewed 1,087 times

0

I got the following jQuery

$(".quantidade_lotes").change(function() {
    var quantidade_linhas = $("#quantidade_linhas").val();
    var quantidade_lotes = $(".quantidade_lotes").val();
    var mensagem = $("#mensagem").val();

    var url = "php/lotes.php?linhas=" + quantidade_linhas + "&lotes=" + quantidade_lotes + "&mensagem=" + mensagem;
    setTimeout(function(){
        $('.lotes_lista').load(url);
    },3000);    
});

I have the message field: By typing a "test" message, I can retrieve and pass it to the url, normal and recover inside PHP via GET, but if I type a larger message, which contains spaces and etc, I can only recover from the field, but I cannot pass the message to the URL.

How can I recover this message with spaces? I thought about turning it into HTML, or some way, so I can pass on this variable?

1 answer

2


I would recommend that you use an ajax call instead of the load.

var quantidade_linhas = $("#quantidade_linhas").val();
var quantidade_lotes = $(".quantidade_lotes").val();
var mensagem = $("#mensagem").val();

$.ajax({
  url: 'php/lotes.php',
  type: 'GET',
  dataType: 'html',
  data: {
    linhas: quantidade_linhas,
    lotes: quantidade_lotes,
    mensagem: mensagem
  },
})
.done(function(ret) {
  console.log("success");
  $('.lotes_lista').html(ret);
})
.fail(function() {
  console.log("error");
})
.always(function() {
  console.log("complete");
});

And this error occurs due to url encoding, which does not accept spaces. Read more about it here https://en.wikipedia.org/wiki/Percent-encoding

Browser other questions tagged

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