Send more than one parameter in GET request

Asked

Viewed 1,023 times

1

I use the code below to send a meter in a request Ajax of the kind GET.

$('#cidades').load('cidades.php?estado=' + $('#estados').val());

How do I also pass a parameter called recurso?

  • What is the variable name?

  • did not understand the difficulty. After all, it is already concatenating a parameter. Then just follow the logic and concatenate another... +"&outro_parametro=valor"

  • The variable name is: resource

  • What is wrong with this concatenation? $('#transacao').load('transacao.php?estado='+$('#estados').val()+'&cliente='+idcl+'&teste='+idcl);

  • Gladison, any answers to your problem? It is interesting to give feedback, because someone else may have the same problem, and when you get here you will know what was done. : ) See here what you can do by getting an answer to your question. ;)

4 answers

10

Another way is to send an object as "second parameter", according to the jQuery.load documentation().

var parametros = {
    estado: $('#estados').val(),
    recurso: $('#recurso').val()
};

$('#cidades').load('cidades.php', parametros, callback);

var callback = function() {
    //do something
}

It is still possible to call a callback function as the third parameter.

  • 3

    +1 for the "second argument". It’s always better to use a jQuery feature in jQuery itself than to concatenate (something jQuery does internally in this case)

5

If you continue with this concatenation there in your code, it would be preferable to use the function $.param jQuery.

var dados = {
   estado: $('#estados').val()
}

var  url = 'cidades.php?' + $.param(dados)

But anyway I consider the answer of @Pedrojuniorcamara better solution

/a/102853/4995

  • +1 for the use of $.param, I did not know this function, it is very useful.

  • 1

    jQuery uses it internally when you use the attribute data ajax as an object ;)

4

You just need to separate the variables with the character &

$('#cidades').load('cidades.php?estado='+$('#estados').val()+'&recurso=seuRecurso');

If you need to use more parameters, I think it’s much better that you do it the way Pedro and Wallace said.

  • Concatenation is the ugliest thing in programming, right, @jbueno ?!?! + 1

  • Yeah? I don’t know @Wallacemaxters

  • Hello, this your script worked perfectly, but so that I can better use these features and implement more parameters, how do I make the IDCL variable used in the parameter to receive a content coming from a php variable? Take a look below: $('#cities'). load('cities.php? status='+$('#states'). val()+'&client='+idcl);

  • @jbueno I don’t like to concatenate (at least in the three languages I most move, I think ugly [python, php, javascript])

  • If you need to use more parameters, I think much You better do it the way Pedro and Wallace said. As for your other question, I think it’s a subject for another question, you can ask as many times as you need (as long as you have common sense, of course).

  • agree, fully. but my doubt now is on how to receive the value of a php variable and put inside the IDCL variable. Because even using the way of the colleagues above, I will always need to receive value of variables of another language.

  • Like I don’t like @Wallacemaxters either, but that doesn’t make me avoid it at all costs. As I assumed that he would need to pass one or two parameters I did so, but as I said, if you start putting more it already gets ugly and has shapes well better to do (as in other answers).

  • That’s another problem @Gladisonneuzaperosini. If it’s another problem, you need another question.

  • @jbueno I understood what I said. After all, even for some types of return I am obliged to concatenate, sometimes in the very libraries I develop.

  • What is wrong with this concatenation? $('#transacao'). load('transacao.php? status='+$('#states'). val()+'&client='+idcl+'&teste='+idcl);

  • What’s the problem with the answer?

Show 6 more comments

3

The parameters should be separator by a "&" thus:

link.php?param1=valor&param2=valor

In your example would look something like:

cidades.php?estado=valor&cidade=valor

Browser other questions tagged

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