1

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

1
  • Don't forget to quote #result. Commented Mar 1, 2013 at 22:32

4 Answers 4

1

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) {
        ...
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

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) {

1 Comment

Don't forget to properly encode value with encodeURIComponent.
0

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

2 Comments

Don't forget to properly encode value with encodeURIComponent.
OMG! I can´t believe it. It´s working great ! All that hours just because of wrong quotes. Thank you ! :-) This is incredible. Should have joinded stackoverflow long before ...
0
$.get('lookup.php?s=<?php echo $id?>&q='+ value,function(data) {

1 Comment

Don't forget to properly encode value with encodeURIComponent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.