Solved

Jquery Autocomplete PHP script

Posted on 2016-11-13
3
45 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 109

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 54

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

777 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