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

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user2095033
  • 27
  • 1
  • 8

4 Answers4

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) {
        ...
    }
});
Blender
  • 289,723
  • 53
  • 439
  • 496
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) {
DarkAjax
  • 15,955
  • 11
  • 53
  • 65
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".

PlantTheIdea
  • 16,061
  • 5
  • 35
  • 40
  • Don't forget to properly encode `value` with `encodeURIComponent`. – Marcel Korpel Mar 01 '13 at 22:30
  • 1
    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 ... – user2095033 Mar 01 '13 at 22:36
0
$.get('lookup.php?s=<?php echo $id?>&q='+ value,function(data) {
Leeish
  • 5,203
  • 2
  • 17
  • 45