Solved

Jquery Autocomplete PHP script

Posted on 2016-11-13
3
21 Views
Last Modified: 2016-11-17
Hello,
Looking at the autocomplete example on Jquery UI, and I cant figure out how the source lets a variable to search the PHP script.

The code can be seen at:
view-source:https://jqueryui.com/resources/demos/autocomplete/remote.html


However, while I see where it sets the search.php, where does it set the variable (search.php?q=) to search the script?
0
Comment
Question by:Stephen Forlance
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 41885248
this file : https://code.jquery.com/ui/1.12.1/jquery-ui.js
contains multiple jquery ui widget, especially the autocomplete widget which start at line 5579

the part interested you is :
	_initSource: function() {
		var array, url,
			that = this;
		if ( $.isArray( this.options.source ) ) {
			array = this.options.source;
			this.source = function( request, response ) {
				response( $.ui.autocomplete.filter( array, request.term ) );
			};
		} else if ( typeof this.options.source === "string" ) {
			url = this.options.source;
			this.source = function( request, response ) {
				if ( that.xhr ) {
					that.xhr.abort();
				}
				that.xhr = $.ajax( {
					url: url,
					data: request,
					dataType: "json",
					success: function( data ) {
						response( data );
					},
					error: function() {
						response( [] );
					}
				} );
			};
		} else {
			this.source = this.options.source;
		}
	},

Open in new window


and some line after,  check line 6 :
	_search: function( value ) {
		this.pending++;
		this._addClass( "ui-autocomplete-loading" );
		this.cancelSearch = false;

		this.source( { term: value }, this._response() );
	}

Open in new window


this is where it define the data to be sent to the server : {term: value}
this autocomplete use "term" and not "q" unlike the other one
1
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 41885257
It doesn't use ?q= -- instead it uses ?term= for the argument.  This is done "under the covers" of the API.  The term is passed in the select function callback.

Docs here:
http://api.jqueryui.com/autocomplete/

Source here:
https://github.com/jquery/jquery-ui/blob/master/demos/autocomplete/search.php

Example here, showing JSON return values for "blue" birds:
https://jqueryui.com/resources/demos/autocomplete/search.php?term=blue
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41887411
If you want to see what is going on under the hood then open the sample page and press F12 to open the console.
Open the console tab and type something into the search box.

You should see something like this
ss92.jpg
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
The viewer will learn how to dynamically set the form action using jQuery.
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)

757 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

18 Experts available now in Live!

Get 1:1 Help Now