PHP Classes

File: appz/type_ahead_db/type_ahead_db.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Classes of Guilherme Blanco  >  pAjax  >  appz/type_ahead_db/type_ahead_db.php  >  Download  
File: appz/type_ahead_db/type_ahead_db.php
Role: Application script
Content type: text/plain
Description: Type Ahead Script
Class: pAjax
Do RPC calls from the browser without page reloads
Author: By
Last change: Corrected MySQL Connection
Date: 9 years ago
Size: 1,829 bytes


Class file image Download

require_once "../../class.pAjax.php";

suggest($text) {
// It could be an SQL statement execution with a like condition, example:
    // SELECT stateName FROM USAStates WHERE stateName LIKE '{$text}%';
mysql_connect("localhost", "username", "password");
$query = "SELECT nome FROM usuarios WHERE nome LIKE '{$text}%'";
$result = mysql_query($query);
$return = array();

    if (
mysql_num_rows($result) > 0) {
        while (
$row = mysql_fetch_array($result)) {
            if (
strtolower($text) == strtolower(substr($row[0], 0, strlen($text))))
$return[] = $row[0];



$AJAX = new pAjax;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

<html xmlns="" lang="en" xml:lang="en">
    <title>Auto Suggestion Type Ahead Example</title>
    <?php $AJAX->showJavaScript("../.."); ?>
<script type="text/javascript" src="autosuggestcontroller.js"></script>
    <script type="text/javascript" src="suggestionprovider.js"></script>
    <script type="text/javascript">
        window.onload = function () {
            var oTextBox = new AutoSuggestController(document.getElementById("TextBox1"), new SuggestionProvider());

    <h1>Type Ahead Suggestion</h1>
      While you type anything on the input, it processes and autosuggest you by completing and selecting the word<br />
      The database of this example are the USA states. Try typing something like "New Mexico" and see what it appears while you type a char.
    <input type="text" id="TextBox1" />