Link to home
Start Free TrialLog in
Avatar of nachtmsk
nachtmskFlag for United States of America

asked on

Jquery autocomplete return data to a script

Hi,
I'm using jquery and autocomplete to query a database of names and return a given name/id.
With the help of EE, I have gotten this to work properly.
I'm at the next stage now, where i need to take the ID I returned, I pass it to a server side script, in this case a perl script.
Can someone show me how I go about doing this in jquery.
Here is the current jquery code I'm using to get the names and return the employeeid to an alert box.
How can i return the employee to a server side script?

<script>

$(document).ready(function(){

$(function() {

$( "#tags" ).autocomplete({
source: "../f/getnames2.pl",
minLength: 2,
select: function(event,ui){
var employeeid = ui.item.empid;

$("#getemp").click(function(employee){
alert(employeeid);
});

}


});


});

});
</script>

Open in new window





Thanks!
Nacht
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Where you have

$("#getemp").click(function(employee){
alert(employeeid);
});

Open in new window


You will instead to an ajax post
$("#getemp").click(function(employee){
// EmployeeID is the field name you are passing
// employeeid is the data being returned from the callback
// you can also make this a separate function.
$.ajax({
  type: "POST",
  url: "some.php",
  data: { EmployeeID: employeeid}
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });

});

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nachtmsk

ASKER

Hi Scott,

Thanks.
I found a similar example online and tried it. I was able to get the alert to show that the POST was successful. I also put the data into an alert and saw the source code of the HTML/data I was returning from the script.
however, how do I return the page to display in the browser and not just in an alert window?
I have this, which works, but only display the response in an alert window.. not a browser.

<script>

$(document).ready(function(){

$(function() {

$( "#tags" ).autocomplete({
source: "../f/getnames2.pl",
minLength: 2,
      select: function(event,ui){
      var employeeid = ui.item.empid;
      
      $("#getemp").click(function(){
      //alert(employeeid);
      $.post("http://www.test.com/f/test.pl",
            {
            
             username:"jsmith",
             guid:"AJJD88AD2-27CC-4D06-B1E7-18EAA80C2957"
            },
            function(data,status){
            alert(status);
            alert(data);
            });
      
            
            
      });
      
      }

      
      });


      });
      
});
</script>
you need to add html element and set the value