-1

these are my two textboxes

<input name="temp" type="text" id="authnames" />
<input name="qot" type="text" id="qid"  value=""/>

and i am gettting value in the first textbox, i can see it in screen (the one with id authnames)

now i need to pass that value to via jquery/ajax to a new php page and need to retrieve it there below is my jquery/ajax code, and see the way i am passing it, is this the correct way, coz idont think i am getting any value in autocompletequote.php, what am i doing wrong?

$(document).ready(function(){
    $("#qid").autocomplete({
        source: "autocompletequote.php",
        minLength: 1,
        data: { postcode: $("#authnames").val(),
        type: "post",
        dataType: "json", 
        success: function(data) {
            console.log( data );
            console.log("hi");
        }
    },
    select: function (event, ui) {
        var label = ui.item.label;
        var value = ui.item.value;
        //alert(label);
        alert(value);
    }
});
});

there are the two codes i used to retrieve value value in "authnames"

$author = $_POST['postcode'];

and

$author = $_GET['postcode'];

is this correct too?

i have written 2 queries in autocomplete.php

one query do not need value of $author and other needs value of $author,

first query is working fine second is not working its returning null

please help me

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Mukund
  • 1,107
  • 2
  • 18
  • 40
  • 1
    You're missing a closing brace after this: `data: { postcode: $("#authnames").val() }, // <- there`. Is that just a typo in your example above? – Rory McCrossan Apr 28 '14 at 07:01
  • @RoryMcCrossan i closed it. its the one with a comma after it, and also i am getting output for this same code for a different query, if the code was wrong i wouldnt get that output ri8? – Mukund Apr 28 '14 at 07:07

2 Answers2

1

I think your syntax for the auto-complete is wrong.

it has to follow jquery-autocomplete 's syntax.

The data is actually a function after the autocomplete.

so to fix ure code, to go towards what u r doing is..

$(document).ready(function(){

$("#qid").autocomplete
({
   source:"autocompletequote.php",
   minLength:1,
   select: function (event, ui) 
   {
    var label = ui.item.label;
    var value = ui.item.value;
    //alert(label);
    alert(value);
    $.ajax({
       url: "autocompletequote.php",
       data: { "Myvariablename" : $("#authnames").val() } ,
       type:"post",
       dataType:"json", 
       success:function(data)
       {
           console.log( data );
           console.log("hi");
       }
     });
   }

  });
 });

Or i am mis-understanding ure purpose

In PHP

$authname = $_POST["Myvariablename"];

EDITED :

To pass an extra query, or custom query, the source attribute has to be a function like so

$("#qid").autocomplete({
  source: function(request, response) {
    $.getJSON("autocompletequote.php", { "Myvariablename" : $("#authnames").val(), "someothervarialble" : xxx }, 
              response);
  },
  minLength: 1,
  select: function(event, ui){
    // your action or futher ajax code goes here.
  }
});
WickStargazer
  • 409
  • 2
  • 10
  • and how to catch it in autocomplete.php? – Mukund Apr 28 '14 at 07:22
  • are you trying to pass the #authnames after passing the #qid and fetch back a list of names+id? or whats the exact purpose? I added how to catch it in php, the second time anyways. Make sure you got if else to see which action you want to perform. – WickStargazer Apr 28 '14 at 07:39
  • i have a query in autocompletequote.php, i that query i need two criterias, one is the letter which i type in that textbox with id "qid" and other is the value which is showing in the textbox with id "authnames", – Mukund Apr 28 '14 at 07:56
  • i need to pass authnames before #qid – Mukund Apr 28 '14 at 08:02
  • I added the source function. Also Refer to this link, http://stackoverflow.com/questions/3693560/how-do-i-pass-an-extra-parameter-to-jquery-autocomplete-field – WickStargazer Apr 28 '14 at 08:08
  • it is like this, i have a query in autocompletequote.php the query is like this `select books from booktable where authorname is "the variable i need to pass explicitly" and books like "which starts with the letters i type in "qid""`, see i just want to get the authornames to this php page. also if i remove the first criteria the page is working fine – Mukund Apr 28 '14 at 08:20
  • means my autocomplete quotes are getting corresponding to the letters i type in, i just need to get that authorname in the php page to get the result, now all books are shown – Mukund Apr 28 '14 at 08:22
0

finally i got the answer i just changed the code to this

<script>
var label;
var value;
 $(document).ready(function(){

                $("#tag").autocomplete({
                    source:'autocomplete.php',
                    minLength:1,
                    select: function (event, ui) 
                    {
                         label = ui.item.label;
                         value = ui.item.value;
                        $("#authnames").val(value);
                        console.log("hi");

                        //alert(label);
                        alert(value);

                        $("#qid").autocomplete({
                       source:'autocompletequote.php?postcode='+value,
                       minLength:1,
                       select: function (event, ui) 
                       {
                        var label1 = ui.item.label;
                        var value1= ui.item.value;

                        console.log("hi");

                        //alert(label);
                        //alert(value);
                       }


                   });

                    }


                });



            });




</script>

now everything is A OK

Mukund
  • 1,107
  • 2
  • 18
  • 40