Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Jquery Autocomplete PHP script

Posted on 2016-11-13
3
Medium Priority
?
154 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 83

Accepted Solution

by:
leakim971 earned 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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 60

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

916 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