jquery add variable to $.get url parameter

3.4k Views Asked by At

I´ve been banging my head against the $.get-function of jquery.

I´m trying to do something like this:

$("#button").click(function(){

var value=$("#textfield").val();

alert(value);

$.get('lookup.php?s=<?php echo $id?>&q=+ value',function(data) {

$(#result).html(data);

This should query lookup.php with GET-parameters:

$id (PHP-variable) & value (jquery/Javascript-variable)

The thing is, that the $id is being filled in correctly, but the "value" of the previous jquery/javascript assignment is not.

Playing with the data:-parameters did not help at all.

Is there a way I can append a jquery-variable from a textfield input to the query string ?

I need to call $.get with those 2 parameters and I cannot find a way in my head to do it otherwise :-).

Hope my intent became clear ...

4

There are 4 best solutions below

0
On BEST ANSWER

You need to put the + value outside of the quotes. Also, I wouldn't build query strings manually. Just pass an object:

$.ajax({
    type: 'get',
    url: 'lookup.php',
    data: {
        s: '<?php echo $id; ?>',
        q: value
    },
    success: function(data) {
        ...
    }
});
1
On

Instead of including the variable value your just including the string 'value', you need to move the variable outside the quotes. It should be like this:

$.get('lookup.php?s=<?php echo $id?>&q=' + value,function(data) {
2
On

Your value should be outside of quotation marks:

$.get('lookup.php?s=<?php echo $id?>&q='+value,function(data) {

When the variable is placed inside of the quotation marks, it is just seen as the string "value".

1
On
$.get('lookup.php?s=<?php echo $id?>&q='+ value,function(data) {