Solved

Jquery Autocomplete PHP script

Posted on 2016-11-13
3
37 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 52

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

867 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