Solved

Jquery autocomplete return data to a script

Posted on 2014-04-14
5
315 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with removing item from Session Array 3 31
Bootstrap date picker V2 date restriction 6 29
Wordpress Query 5 25
Ajax and PHP 9 29
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

809 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