Solved

Jquery autocomplete return data to a script

Posted on 2014-04-14
5
312 Views
Last Modified: 2014-05-15
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
0
Comment
Question by:nachtmsk
  • 2
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39999241
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

0
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 39999248
I forgot to mention, the example I used is directly from https://api.jquery.com/jQuery.ajax/

Instead of posting to a php page, it would be your script.
0
 

Author Comment

by:nachtmsk
ID: 39999259
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?
0
 

Author Comment

by:nachtmsk
ID: 39999271
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>
0
 
LVL 2

Expert Comment

by:vogen
ID: 40050551
you need to add html element and set the value
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now