Search code examples
phpjavascriptjqueryjquery-get

jquery add variable to $.get url parameter


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 ...


Solution

  • 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) {
            ...
        }
    });