Source for file autocomplete.php
Documentation is available at autocomplete.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Matteo Di Giovinazzo <matteodg@infinito.it> |
// | For the JavaScript code thanks to Martin Honnen and |
// | http://www.faqts.com/knowledge_base/view.phtml/aid/13562 |
// | http://www.sitepoint.com/article/1220 |
// +----------------------------------------------------------------------+
// $Id: autocomplete.php,v 1.6 2005/08/05 16:33:56 avb Exp $
require_once("HTML/QuickForm/text.php");
* Class to dynamically create an HTML input text element that
* at every keypressed javascript event, check in an array of options
* if there's a match and autocomplete the text in case of match.
* $autocomplete =& $form->addElement('autocomplete', 'fruit', 'Favourite fruit:');
* $options = array("Apple", "Orange", "Pear", "Strawberry");
* $autocomplete->setOptions($options);
* @author Matteo Di Giovinazzo <matteodg@infinito.it>
* Options for the autocomplete input text element
* "One-time" javascript (containing functions), see bug #4611
* @param string $elementName (optional)Input field name attribute
* @param string $elementLabel (optional)Input field label in form
* @param array $options (optional)Autocomplete options
* @param mixed $attributes (optional)Either a typical HTML attribute string
* or an associative array. Date format is passed along the attributes.
$this->HTML_QuickForm_text ($elementName, $elementLabel, $attributes);
$this->_persistantFreeze = true;
$this->_type = 'autocomplete';
* Sets the options for the autocomplete input text element
* @param array $options Array of options for the autocomplete input text element
* Returns Html for the autocomplete input text element
// prevent problems with grouped elements
$arrayName = str_replace(array ('[', ']'), array ('__', ''), $this->getName ()) . '_values';
$this->updateAttributes (array (
'onkeypress' => 'return autocomplete(this, event, ' . $arrayName . ');'
if ($this->_flagFrozen) {
$js = "<script type=\"text/javascript\">\n//<![CDATA[\n";
if (!defined('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS')) {
/* begin javascript for autocomplete */
function setSelectionRange(input, selectionStart, selectionEnd) {
if (input.setSelectionRange) {
input.setSelectionRange(selectionStart, selectionEnd);
else if (input.createTextRange) {
var range = input.createTextRange();
range.moveEnd("character", selectionEnd);
range.moveStart("character", selectionStart);
function setCaretToPosition(input, position) {
setSelectionRange(input, position, position);
function replaceSelection (input, replaceString) {
var len = replaceString.length;
if (input.setSelectionRange) {
var selectionStart = input.selectionStart;
var selectionEnd = input.selectionEnd;
input.value = input.value.substring(0, selectionStart) + replaceString + input.value.substring(selectionEnd);
input.selectionStart = selectionStart + len;
input.selectionEnd = selectionStart + len;
else if (document.selection) {
var range = document.selection.createRange();
var saved_range = range.duplicate();
if (range.parentElement() == input) {
range.text = replaceString;
range.moveEnd("character", saved_range.selectionStart + len);
range.moveStart("character", saved_range.selectionStart + len);
function autocompleteMatch (text, values) {
for (var i = 0; i < values.length; i++) {
if (values[i].toUpperCase().indexOf(text.toUpperCase()) == 0) {
function autocomplete(textbox, event, values) {
if (textbox.setSelectionRange || textbox.createTextRange) {
var c = String.fromCharCode(
(event.charCode == undefined) ? event.keyCode : event.charCode
replaceSelection(textbox, c);
sMatch = autocompleteMatch(textbox.value, values);
var len = textbox.value.length;
setSelectionRange(textbox, len, textbox.value.length);
/* end javascript for autocomplete */
define('HTML_QUICKFORM_AUTOCOMPLETE_EXISTS', true );
$js .= 'var ' . $arrayName . " = new Array();\n";
for ($i = 0; $i < count($this->_options); $i++ ) {
$js .= $arrayName . '[' . $i . "] = '" . strtr($this->_options[$i], $jsEscape) . "';\n";
$js .= "//]]>\n</script>";
return $js . parent ::toHtml ();
} // end class HTML_QuickForm_autocomplete
Documentation generated on Mon, 11 Mar 2019 14:16:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|