Location: PHPKode > projects > PRAjax - PHP Reflected Ajax > examples/example_component_suggest.php.txt
<?php
// Set the header
header("Content-Type: text/html; charset=utf-8");

// Use bin or src?
$strSources = '../bin';

// Import PRAjax
require_once($strSources . '/PRAjax.php');

// Instantiate PRAjax
$objPRAjax = new PRAjax($strSources . '/');

// List of country names
$aCountries = array();
$aCountries[] = "AFGHANISTAN";
$aCountries[] = "ALAND ISLANDS";
$aCountries[] = "ALBANIA";
$aCountries[] = "ALGERIA";
$aCountries[] = "AMERICAN SAMOA";
$aCountries[] = "ANDORRA";
$aCountries[] = "ANGOLA";
$aCountries[] = "ANGUILLA";
$aCountries[] = "ANTARCTICA";
$aCountries[] = "ANTIGUA AND BARBUDA";
$aCountries[] = "ARGENTINA";
$aCountries[] = "ARMENIA";
$aCountries[] = "ARUBA";
$aCountries[] = "AUSTRALIA";
$aCountries[] = "AUSTRIA";
$aCountries[] = "AZERBAIJAN";
$aCountries[] = "BAHAMAS";
$aCountries[] = "BAHRAIN";
$aCountries[] = "BANGLADESH";
$aCountries[] = "BARBADOS";
$aCountries[] = "BELARUS";
$aCountries[] = "BELGIUM";
$aCountries[] = "BELIZE";
$aCountries[] = "BENIN";
$aCountries[] = "BERMUDA";
$aCountries[] = "BHUTAN";
$aCountries[] = "BOLIVIA";
$aCountries[] = "BOSNIA AND HERZEGOVINA";
$aCountries[] = "BOTSWANA";
$aCountries[] = "BOUVET ISLAND";
$aCountries[] = "BRAZIL";
$aCountries[] = "BRITISH INDIAN OCEAN TERRITORY";
$aCountries[] = "BRUNEI DARUSSALAM";
$aCountries[] = "BULGARIA";
$aCountries[] = "BURKINA FASO";
$aCountries[] = "BURUNDI";
$aCountries[] = "CAMBODIA";
$aCountries[] = "CAMEROON";
$aCountries[] = "CANADA";
$aCountries[] = "CAPE VERDE";
$aCountries[] = "CAYMAN ISLANDS";
$aCountries[] = "CENTRAL AFRICAN REPUBLIC";
$aCountries[] = "CHAD";
$aCountries[] = "CHILE";
$aCountries[] = "CHINA";
$aCountries[] = "CHRISTMAS ISLAND";
$aCountries[] = "COCOS (KEELING) ISLANDS";
$aCountries[] = "COLOMBIA";
$aCountries[] = "COMOROS";
$aCountries[] = "CONGO";
$aCountries[] = "CONGO, THE DEMOCRATIC REPUBLIC OF THE";
$aCountries[] = "COOK ISLANDS";
$aCountries[] = "COSTA RICA";
$aCountries[] = "CÔTE D'IVOIRE";
$aCountries[] = "CROATIA";
$aCountries[] = "CUBA";
$aCountries[] = "CYPRUS";
$aCountries[] = "CZECH REPUBLIC";
$aCountries[] = "DENMARK";
$aCountries[] = "DJIBOUTI";
$aCountries[] = "DOMINICA";
$aCountries[] = "DOMINICAN REPUBLIC";
$aCountries[] = "ECUADOR";
$aCountries[] = "EGYPT";
$aCountries[] = "EL SALVADOR";
$aCountries[] = "EQUATORIAL GUINEA";
$aCountries[] = "ERITREA";
$aCountries[] = "ESTONIA";
$aCountries[] = "ETHIOPIA";
$aCountries[] = "FALKLAND ISLANDS (MALVINAS)";
$aCountries[] = "FAROE ISLANDS";
$aCountries[] = "FIJI";
$aCountries[] = "FINLAND";
$aCountries[] = "FRANCE";
$aCountries[] = "FRENCH GUIANA";
$aCountries[] = "FRENCH POLYNESIA";
$aCountries[] = "FRENCH SOUTHERN TERRITORIES";
$aCountries[] = "GABON";
$aCountries[] = "GAMBIA";
$aCountries[] = "GEORGIA";
$aCountries[] = "GERMANY";
$aCountries[] = "GHANA";
$aCountries[] = "GIBRALTAR";
$aCountries[] = "GREECE";
$aCountries[] = "GREENLAND";
$aCountries[] = "GRENADA";
$aCountries[] = "GUADELOUPE";
$aCountries[] = "GUAM";
$aCountries[] = "GUATEMALA";
$aCountries[] = "GUINEA";
$aCountries[] = "GUINEA-BISSAU";
$aCountries[] = "GUYANA";
$aCountries[] = "HAITI";
$aCountries[] = "HEARD ISLAND AND MCDONALD ISLANDS";
$aCountries[] = "HOLY SEE (VATICAN CITY STATE)";
$aCountries[] = "HONDURAS";
$aCountries[] = "HONG KONG";
$aCountries[] = "HUNGARY";
$aCountries[] = "ICELAND";
$aCountries[] = "INDIA";
$aCountries[] = "INDONESIA";
$aCountries[] = "IRAN, ISLAMIC REPUBLIC OF";
$aCountries[] = "IRAQ";
$aCountries[] = "IRELAND";
$aCountries[] = "ISRAEL";
$aCountries[] = "ITALY";
$aCountries[] = "JAMAICA";
$aCountries[] = "JAPAN";
$aCountries[] = "JORDAN";
$aCountries[] = "KAZAKHSTAN";
$aCountries[] = "KENYA";
$aCountries[] = "KIRIBATI";
$aCountries[] = "KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF";
$aCountries[] = "KOREA, REPUBLIC OF";
$aCountries[] = "KUWAIT";
$aCountries[] = "KYRGYZSTAN";
$aCountries[] = "LAO PEOPLE'S DEMOCRATIC REPUBLIC";
$aCountries[] = "LATVIA";
$aCountries[] = "LEBANON";
$aCountries[] = "LESOTHO";
$aCountries[] = "LIBERIA";
$aCountries[] = "LIBYAN ARAB JAMAHIRIYA";
$aCountries[] = "LIECHTENSTEIN";
$aCountries[] = "LITHUANIA";
$aCountries[] = "LUXEMBOURG";
$aCountries[] = "MACAO";
$aCountries[] = "MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF";
$aCountries[] = "MADAGASCAR";
$aCountries[] = "MALAWI";
$aCountries[] = "MALAYSIA";
$aCountries[] = "MALDIVES";
$aCountries[] = "MALI";
$aCountries[] = "MALTA";
$aCountries[] = "MARSHALL ISLANDS";
$aCountries[] = "MARTINIQUE";
$aCountries[] = "MAURITANIA";
$aCountries[] = "MAURITIUS";
$aCountries[] = "MAYOTTE";
$aCountries[] = "MEXICO";
$aCountries[] = "MICRONESIA, FEDERATED STATES OF";
$aCountries[] = "MOLDOVA, REPUBLIC OF";
$aCountries[] = "MONACO";
$aCountries[] = "MONGOLIA";
$aCountries[] = "MONTSERRAT";
$aCountries[] = "MOROCCO";
$aCountries[] = "MOZAMBIQUE";
$aCountries[] = "MYANMAR";
$aCountries[] = "NAMIBIA";
$aCountries[] = "NAURU";
$aCountries[] = "NEPAL";
$aCountries[] = "NETHERLANDS";
$aCountries[] = "NETHERLANDS ANTILLES";
$aCountries[] = "NEW CALEDONIA";
$aCountries[] = "NEW ZEALAND";
$aCountries[] = "NICARAGUA";
$aCountries[] = "NIGER";
$aCountries[] = "NIGERIA";
$aCountries[] = "NIUE";
$aCountries[] = "NORFOLK ISLAND";
$aCountries[] = "NORTHERN MARIANA ISLANDS";
$aCountries[] = "NORWAY";
$aCountries[] = "OMAN";
$aCountries[] = "PAKISTAN";
$aCountries[] = "PALAU";
$aCountries[] = "PALESTINIAN TERRITORY, OCCUPIED";
$aCountries[] = "PANAMA";
$aCountries[] = "PAPUA NEW GUINEA";
$aCountries[] = "PARAGUAY";
$aCountries[] = "PERU";
$aCountries[] = "PHILIPPINES";
$aCountries[] = "PITCAIRN";
$aCountries[] = "POLAND";
$aCountries[] = "PORTUGAL";
$aCountries[] = "PUERTO RICO";
$aCountries[] = "QATAR";
$aCountries[] = "RÉUNION";
$aCountries[] = "ROMANIA";
$aCountries[] = "RUSSIAN FEDERATION";
$aCountries[] = "RWANDA";
$aCountries[] = "SAINT HELENA";
$aCountries[] = "SAINT KITTS AND NEVIS";
$aCountries[] = "SAINT LUCIA";
$aCountries[] = "SAINT PIERRE AND MIQUELON";
$aCountries[] = "SAINT VINCENT AND THE GRENADINES";
$aCountries[] = "SAMOA";
$aCountries[] = "SAN MARINO";
$aCountries[] = "SAO TOME AND PRINCIPE";
$aCountries[] = "SAUDI ARABIA";
$aCountries[] = "SENEGAL";
$aCountries[] = "SERBIA AND MONTENEGRO";
$aCountries[] = "SEYCHELLES";
$aCountries[] = "SIERRA LEONE";
$aCountries[] = "SINGAPORE";
$aCountries[] = "SLOVAKIA";
$aCountries[] = "SLOVENIA";
$aCountries[] = "SOLOMON ISLANDS";
$aCountries[] = "SOMALIA";
$aCountries[] = "SOUTH AFRICA";
$aCountries[] = "SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS";
$aCountries[] = "SPAIN";
$aCountries[] = "SRI LANKA";
$aCountries[] = "SUDAN";
$aCountries[] = "SURINAME";
$aCountries[] = "SVALBARD AND JAN MAYEN";
$aCountries[] = "SWAZILAND";
$aCountries[] = "SWEDEN";
$aCountries[] = "SWITZERLAND";
$aCountries[] = "SYRIAN ARAB REPUBLIC";
$aCountries[] = "TAIWAN, PROVINCE OF CHINA";
$aCountries[] = "TAJIKISTAN";
$aCountries[] = "TANZANIA, UNITED REPUBLIC OF";
$aCountries[] = "THAILAND";
$aCountries[] = "TIMOR-LESTE";
$aCountries[] = "TOGO";
$aCountries[] = "TOKELAU";
$aCountries[] = "TONGA";
$aCountries[] = "TRINIDAD AND TOBAGO";
$aCountries[] = "TUNISIA";
$aCountries[] = "TURKEY";
$aCountries[] = "TURKMENISTAN";
$aCountries[] = "TURKS AND CAICOS ISLANDS";
$aCountries[] = "TUVALU";
$aCountries[] = "UGANDA";
$aCountries[] = "UKRAINE";
$aCountries[] = "UNITED ARAB EMIRATES";
$aCountries[] = "UNITED KINGDOM";
$aCountries[] = "UNITED STATES";
$aCountries[] = "UNITED STATES MINOR OUTLYING ISLANDS";
$aCountries[] = "URUGUAY";
$aCountries[] = "UZBEKISTAN";
$aCountries[] = "VANUATU";
$aCountries[] = "VENEZUELA";
$aCountries[] = "VIET NAM";
$aCountries[] = "VIRGIN ISLANDS, BRITISH";
$aCountries[] = "VIRGIN ISLANDS, U.S.";
$aCountries[] = "WALLIS AND FUTUNA";
$aCountries[] = "WESTERN SAHARA";
$aCountries[] = "YEMEN";
$aCountries[] = "ZAMBIA";
$aCountries[] = "ZIMBABWE";

// Functions
function CountriesStartWith($pBegin) {
	global $aCountries;
	
	$aResult = array();
	foreach ($aCountries as $country) {
    if (substr(strtolower($country), 0, strlen($pBegin)) == strtolower($pBegin)) {
			$aResult[] = $country;
		}
	}
	
	return $aResult;
}

function Suggest($pValue) {
	return CountriesStartWith($pValue);
}

function SuggestYear($pValue) {
      $aArray = array();
      for ($i = date('Y'); $i >= 1900; $i--) {
            if (substr($i, 0, strlen($pValue)) == $pValue) {
                  array_push($aArray, $i);
            }
      }
	return $aArray;
}

// Register functions
$objPRAjax->RegisterFunction('Suggest');
$objPRAjax->RegisterFunction('SuggestYear');

// Handle PRAjax client request
$objPRAjax->HandleClientRequest();
?>
<html>
<head>
	<title>Auto-suggest</title>	
	<?php $objPRAjax->GetJavaScript(); ?>
	<script language="JavaScript" src="<?=$strSources?>/prajax_component_suggest.js"></script>
	<style type="text/css" media="all">
            @import "<?=$strSources?>/prajax_component_suggest.css";
      </style>
	
</head>
<body>
<div style="width: 100%; height: 65px; background-color: #EEEEEE; font-family: Arial, Helvetica, Sans-Serif; font-size: 9pt;">
This example shows a Google-suggest-like text field, which shows a list of possible values upon typing. These values are loaded while you type, using a PRAjax back-end and the PRAjax suggest component (adding an attribute to your text field is enough). Try filling in your country name.
<br>
<a href="example_component_suggest.php.txt" target="_blank">View code...</a>
</div>
<div id="prajax_status" style="position: absolute; right:1px; top: 1px;"></div>
<form name="frmCountry" style="font-family: Arial, Helvetica, sans-serif; font-size: 11px;">
Your country:<br>
<input type="text" name="txtCountry" id="txtCountry" prajax_suggest="Suggest" style="width: 150px;"><br>
Your birth year:<br>
<input type="text" name="txtBirthYear" id="txtBirthYear" prajax_suggest="SuggestYear" style="width: 150px;"><br>
</form>
</body>
</html>
Return current item: PRAjax - PHP Reflected Ajax