Solved

Jquery autocomplete return data to a script

Posted on 2014-04-14
5
311 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
you need to add html element and set the value
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now