Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Jul 17, 2022. It is now read-only.

AJAX Live Search is a PHP search form that similar to Google Autocomplete feature displays the result as you type.

License

NotificationsYou must be signed in to change notification settings

iranianpep/ajax-live-search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

*** This is not maintained anymore ***I started this project a few years ago, but a lot has been changed so far. Also, because of the coupling between different layers, and also old technologies (jQuery!) this is not a scalable project. Thanks a lot for all the support but unfortunately I have to archive this project :(

AJAX Live Search is a jQuery plugin / PHP search form that searches and displays the result as you type similar to Google Autocomplete feature.

Demo

Check it out in action.

Browser Support

IEChromeFirefoxOperaSafari
IE 8+ ✔Chrome ✔Firefox ✔Opera ✔Safari ✔

Thanks toBrowserStack andJetBrains for supporting this project.

Getting started with Ajax Live Search

Getting the current example inindex.php to work including creating a dummy database and a table should not take longer than 7 minutes. But it will take more in case you need to integrate it in an existing project. To achieve that assuming you have this text field:

<input type="text" class='mySearch' placeholder="Type to start searching ...">

  1. Copy the folders includingcore,css,font,img,js andtemplates to your project.

  2. Specify the required configurations specially database configurations incore/Config.template.php and rename the file toConfig.php. This file contains all the back-end settings for the plugin that have been explained in PHP Configs table.

  3. Includejs/ajaxlivesearch.min.js orjs/ajaxlivesearch.js andcss/ajaxlivesearch.min.css orcss/ajaxlivesearch.css in your page.

  4. Change the URL forAccess-Control-Allow-Origin header incore/AjaxProcessor.php to your project URL. Currently it ishttps://ajaxlivesearch.com.

  5. Make surecore/Handler.php andcore/Config.php are included in your (PHP) page and you have these lines at the very top of the file (Checkindex.php):

    use AjaxLiveSearch\core\Config;use AjaxLiveSearch\core\Handler;if (session_id() == '') {    session_start();}$handler = new Handler();$handler->getJavascriptAntiBot();
  6. Lastly, hook the plugin to the text field and pass the required options (loaded_at & token):

jQuery("#ls_query").ajaxlivesearch({loaded_at: ,token: getToken() . "'"; ?>,max_input: ,});```

You can also post additional parameters to the server if you need. To achieve this you should adddata attributes to the search input:

<input type="text" class='mySearch' placeholder="Type to start searching ..." data-additionalData="hello world!">

For example, in this case you can access the data attribute in PHP like this:

// key is transformed to lowercase$additionalData = $_POST['additionaldata'];

jQuery Options

NameTypeRequiredDescription
loaded_atIntegerYesThis is used to prevent bots from searching.
tokenStringYesThis is used to prevent CSRF attack.
urlStringNoDefault: ajax/process_livesearch.php.
cacheBooleanNoThis refers to Ajax request caching. Default: false
form_anti_botStringNoDefault: ajaxlivesearch_guard
slide_speedStringNoDefault: fast
type_delayIntegerNoDefault: 350
max_inputIntegerNoDefault: 20
min_chars_to_searchIntegerNoMinimum characters length to start searching. Default: 0
page_rangesArrayNoDefault: [0, 5, 10]
page_range_defaultIntegerNoDefault: 5
form_anti_bot_classStringNoDefault: ls_anti_bot
footer_classStringNoDefault: ls_result_footer
next_page_classStringNoDefault: ls_next_page
previous_page_classStringNoDefault: ls_previous_page
page_limitStringNoDefault: page_limit
result_wrapper_classStringNoDefault: ls_result_div
result_classStringNoDefault: ls_result_main
container_classStringNoDefault: ls_container
pagination_classStringNoDefault: pagination
form_classStringNoDefault: search
loaded_at_classStringNoDefault: ls_page_loaded_at
token_classStringNoDefault: ls_token
current_page_hidden_classStringNoDefault: ls_current_page
current_page_lbl_classStringNoDefault: ls_current_page_lbl
last_page_lbl_classStringNoDefault: ls_last_page_lbl
total_page_lbl_classStringNoDefault: ls_last_page_lbl
page_range_classStringNoDefault: ls_items_per_page
navigation_classStringNoDefault: navigation
arrow_classStringNoDefault: arrow

Custom Event

Name
onResultClick
onResultEnter
onAjaxComplete

Example:

jQuery(".mySearch").ajaxlivesearch({    loaded_at: <?php echo time(); ?>,    token: <?php echo "'" . $handler->getToken() . "'"; ?>,    max_input: <?php echo Config::getConfig('maxInputLength'); ?>,    onResultClick: function(e, data) {        // get the index 1 (second column) value        var selectedOne = jQuery(data.selected).find('td').eq('1').text();        // set the input value        jQuery('#ls_query').val(selectedOne);        // hide the result        jQuery("#ls_query").trigger('ajaxlivesearch:hide_result');    },    onResultEnter: function(e, data) {        // do whatever you want        // jQuery("#ls_query").trigger('ajaxlivesearch:search', {query: 'test'});    },    onAjaxComplete: function(e, data) {    }});

Custom Trigger

Name
ajaxlivesearch:hide_result
ajaxlivesearch:search

PHP Configurations

NameTypeRequiredDescription
dataSourcesArrayYesData source for each search text field. Keys are refering to the field HTML id. Currently MySQL and mongoDB (this is in beta) are supported.

MySQL data source configs:
NameTypeRequiredDescription
hostStringYesMySQL database host. It usually is 'localhost'.
databaseStringYesMySQL database name.
usernameStringYesMySQL database username.
passStringYesMySQL database username password.
tableStringYesMySQL database table that the live search searches in.
searchColumnsArrayYesSearch columns that the live search searches in. It can be one or many. e.g. array('column_name_1', 'column_name_2')
orderByStringNoColumn that the result is ordered based in it.
orderDirectionStringNoOrder direction: 'ASC' or 'DESC' for 'orderBy'. Default value: ASC
filterResultArrayNoColumns that need to be in the result. If it is empty all the columns will be returned.
comparisonOperatorStringYesSearch query comparison operator. Possible values for comparison operators are: 'LIKE' and '='.
searchPatternStringYesThis is used to specify how the query is searched. possible values are: `q`, `*q`, `q*`, `*q*`.
caseSensitiveStringYesSearch query case sensitivity
maxResultIntegerNoThis is used to limit the maximum number of result.
displayHeaderArrayNoThis is used to display or hide the header in the result. If 'active' is set to true header is displayed. Also, it is possible to map columns to different titles.
typeStringYesType of the datasource. Currently possible values are: 'mysql' or 'mongo'.
antiBotStringYesThis is used as a security technique to prevent form submissions from those bots that do not use JavaScript. In this technique, a hidden field is populated using jQuery with this value. It can have any value, but it MUST be the same as `form_anti_bot` option passed to the jQuery plugin. By default it is set to `ajaxlivesearch_guard`.
searchStartTimeOffsetIntegerYesThis is used for another security technique against bots. Some bots immediately submit a form once the page is finished loading. However, for human beings it takes more time to fill a field. By default this parameter is set to 3 seconds. Assigning more than 3 seconds is not recommended.
maxInputLengthIntegerYesThis specifies the maximum length of characters in search field.
templateStringYesThis specifies the template name located in templates folder

FAQ

  • What is the risk in disabling anti_bot and token?

    form_anti_bot is a nice security feature to have but it should be fine to remove it. To achieve this you need to changevalidateRequest() incore/AjaxProcessor.php and remove$_POST['ls_anti_bot'] as well as changingverifySessionValue($sessionParameter, $sessionValue) incore/Handler.php. In contrast, removing thetoken allows anyone to create a form and use your server as the data source and therefore it is not recommended. Currently, each time the page / search form is loaded a new token is generated by callinggetToken() in the form page (index.php in the current example). Then the token and anti_bot are checked usingverifySessionValue($sessionParameter, $sessionValue) upon searching / sending a new request to the server. If this looks an overkill to you, ingetToken() return the same token for the existing session.

  • How a column can be hidden in the result?

    This can be achieved in your template. An example can be found intemplates/hiddenColumn.php

License

MIT License

Buy me half of a coffee if you like!

Donate

About

AJAX Live Search is a PHP search form that similar to Google Autocomplete feature displays the result as you type.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp