473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

type ahead and autofill multiple fields

What I would like to do is have the user start typing in the last name
and as they were typing it would retrieve names from the database and
give a drop down like autosuggest in google. That much I have figured
out. But what I really want is once the name is in the field I would
like it to retrieve the rest of the information from the database ie.
firstname address etc. and put it other fields on the form. Is this
possible?

Sep 1 '06 #1
4 6082
ja*******@gmail.com said the following on 9/1/2006 12:36 PM:
What I would like to do is have the user start typing in the last name
and as they were typing it would retrieve names from the database and
give a drop down like autosuggest in google. That much I have figured
out. But what I really want is once the name is in the field I would
like it to retrieve the rest of the information from the database ie.
firstname address etc. and put it other fields on the form. Is this
possible?
Yes.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 1 '06 #2

Randy Webb wrote:
ja*******@gmail.com said the following on 9/1/2006 12:36 PM:
What I would like to do is have the user start typing in the last name
and as they were typing it would retrieve names from the database and
give a drop down like autosuggest in google. That much I have figured
out. But what I really want is once the name is in the field I would
like it to retrieve the rest of the information from the database ie.
firstname address etc. and put it other fields on the form. Is this
possible?

Yes.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Thanks Randy,

But how. I am able to do the autosuggest no problem and the fields do
fill in no problem either. But what is happening is when I use my
mouse to select from the autosuggest dropdown the other fields do not
update right away. I thought that if I use the onblur it would work.
In order for it to fill in the other fields I have to click on another
field and then come back to the autosuggest field and then click on
another field again. But if I don't use the mouse to select from the
dropdown it will work. What am I missing here.

Thanks,

Here is what I have so far;
------------------------The Form-------------------

<div class="row">
<span class="label">Client</span><span
class="formw"><table ><tr><td>Last Name</td><td>First
Name</td><td>Age</td><td>In Care</td><tr>
<td><label for="clientID">Client ID:</label></td>
<td><input name="clientID" id="clientID" size="5" maxlength="4"
onkeyup="ajax_showOptions(this,'getCountriesByLett ers',event)"
onblur="initFormEvents();"></td>
<td><input name="firstname" id="firstname" size="20"
maxlength="255" onmousedown="initFormEvents1();" ></td>
<td><input name="lastname" id="lastname" size="20"
maxlength="255"></td>
<td><input name="address" id="address" size="20"
maxlength="255"></td>
<td><input name="zipCode" id="zipCode" size="4" maxlength="5"></td>
<td><input name="city" id="city" size="20" maxlength="255"></td>
<td><input name="country" id="country" size="20"
maxlength="255"></td>
</tr></table></span>
</div>

-------------------The Script to fill in the fields------------------

var ajax = new sack();
var currentClientID=false;
function getClientData()
{
var clientId =
document.getElementById('clientID').value.replace(/[^0-9]/g,'');
if(clientId.length==4 && clientId!=currentClientID){
currentClientID = clientId
ajax.requestFile = 'getClient.php?getClientId='+clientId; //
Specifying which file to get
ajax.onCompletion = showClientData; // Specify function that will be
executed after file has been found
ajax.runAJAX(); // Execute AJAX function

}

}

function showClientData()
{
var formObj = document.forms['form1'];
eval(ajax.response);
}
function initFormEvents()
{
document.getElementById('clientID').onblur = getClientData;
document.getElementById('clientID').focus();
}
------------------The auto suggest Script--------------------
var ajaxBox_offsetX = 0;
var ajaxBox_offsetY = 0;
var ajax_list_externalFile = 'ajax-list-countries.php'; // Path to
external file
var minimumLettersBeforeLookup = 1; // Number of letters entered
before a lookup is performed.

var ajax_list_objects = new Array();
var ajax_list_cachedLists = new Array();
var ajax_list_activeInput = false;
var ajax_list_activeItem;
var ajax_list_optionDivFirstItem = false;
var ajax_list_currentLetters = new Array();
var ajax_optionDiv = false;
var ajax_optionDiv_iframe = false;

var ajax_list_MSIE = false;
if(navigator.userAgent.indexOf('MSIE')>=0 &&
navigator.userAgent.indexOf('Opera')<0)ajax_list_M SIE=true;

function ajax_getTopPos(inputObj)
{

var returnValue = inputObj.offsetTop;
while((inputObj = inputObj.offsetParent) != null){
returnValue += inputObj.offsetTop;
}
return returnValue;
}
function ajax_list_cancelEvent()
{
return false;
}

function ajax_getLeftPos(inputObj)
{
var returnValue = inputObj.offsetLeft;
while((inputObj = inputObj.offsetParent) != null)returnValue +=
inputObj.offsetLeft;

return returnValue;
}

function ajax_option_setValue(e,inputObj)
{
if(!inputObj)inputObj=this;
var tmpValue = inputObj.innerHTML;
if(ajax_list_MSIE)tmpValue = inputObj.innerText;else tmpValue =
inputObj.textContent;
if(!tmpValue)tmpValue = inputObj.innerHTML;
ajax_list_activeInput.value = tmpValue;
if(document.getElementById(ajax_list_activeInput.n ame +
'_hidden'))document.getElementById(ajax_list_activ eInput.name +
'_hidden').value = inputObj.id;
ajax_options_hide();
}

function ajax_options_hide()
{
ajax_optionDiv.style.display='none';
if(ajax_optionDiv_iframe)ajax_optionDiv_iframe.sty le.display='none';
}

function ajax_options_rollOverActiveItem(item,fromKeyBoard)
{
if(ajax_list_activeItem)ajax_list_activeItem.class Name='optionDiv';
item.className='optionDivSelected';
ajax_list_activeItem = item;

if(fromKeyBoard){
if(ajax_list_activeItem.offsetTop>ajax_optionDiv.o ffsetHeight){
ajax_optionDiv.scrollTop = ajax_list_activeItem.offsetTop -
ajax_optionDiv.offsetHeight + ajax_list_activeItem.offsetHeight + 2 ;
}
if(ajax_list_activeItem.offsetTop<ajax_optionDiv.s crollTop)
{
ajax_optionDiv.scrollTop = 0;
}
}
}

function ajax_option_list_buildList(letters,paramToExternal File)
{

ajax_optionDiv.innerHTML = '';
ajax_list_activeItem = false;
if(ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()].length<=1){
ajax_options_hide();
return;
}

ajax_list_optionDivFirstItem = false;
var optionsAdded = false;
for(var
no=0;no<ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()].length;no++){
if(ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()][no].length==0)continue;
optionsAdded = true;
var div = document.createElement('DIV');
var items =
ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()][no].split(/###/gi);

if(ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()].length==1
&& ajax_list_activeInput.value == items[0]){
ajax_options_hide();
return;
}
div.innerHTML = items[items.length-1];
div.id = items[0];
div.className='optionDiv';
div.onmouseover = function(){
ajax_options_rollOverActiveItem(this,false) }
div.onclick = ajax_option_setValue;
if(!ajax_list_optionDivFirstItem)ajax_list_optionD ivFirstItem = div;
ajax_optionDiv.appendChild(div);
}
if(optionsAdded){
ajax_optionDiv.style.display='block';
if(ajax_optionDiv_iframe)ajax_optionDiv_iframe.sty le.display='';
}

}

function
ajax_option_list_showContent(ajaxIndex,inputObj,pa ramToExternalFile)
{
var letters = inputObj.value;
var content = ajax_list_objects[ajaxIndex].response;
var elements = content.split('|');
ajax_list_cachedLists[paramToExternalFile][letters.toLowerCase()] =
elements;
ajax_option_list_buildList(letters,paramToExternal File);

}

function ajax_option_resize(inputObj)
{
ajax_optionDiv.style.top = (ajax_getTopPos(inputObj) +
inputObj.offsetHeight + ajaxBox_offsetY) + 'px';
ajax_optionDiv.style.left = (ajax_getLeftPos(inputObj) +
ajaxBox_offsetX) + 'px';
if(ajax_optionDiv_iframe){
ajax_optionDiv_iframe.style.left = ajax_optionDiv.style.left;
ajax_optionDiv_iframe.style.top = ajax_optionDiv.style.top;
}

}

function ajax_showOptions(inputObj,paramToExternalFile,e)
{

if(e.keyCode==13 || e.keyCode==9)return;
if(ajax_list_currentLetters[inputObj.name]==inputObj.value)return;
if(!ajax_list_cachedLists[paramToExternalFile])ajax_list_cachedLists[paramToExternalFile]
= new Array();
ajax_list_currentLetters[inputObj.name] = inputObj.value;
if(!ajax_optionDiv){
ajax_optionDiv = document.createElement('DIV');
ajax_optionDiv.id = 'ajax_listOfOptions';
document.body.appendChild(ajax_optionDiv);

if(ajax_list_MSIE){
ajax_optionDiv_iframe = document.createElement('IFRAME');
ajax_optionDiv_iframe.border='0';
ajax_optionDiv_iframe.style.width = ajax_optionDiv.clientWidth +
'px';
ajax_optionDiv_iframe.style.height = ajax_optionDiv.clientHeight +
'px';
ajax_optionDiv_iframe.id = 'ajax_listOfOptions_iframe';

document.body.appendChild(ajax_optionDiv_iframe);
}

var allInputs = document.getElementsByTagName('INPUT');
for(var no=0;no<allInputs.length;no++){
if(!allInputs[no].onkeyup)allInputs[no].onfocus =
ajax_options_hide;
}
var allSelects = document.getElementsByTagName('SELECT');
for(var no=0;no<allSelects.length;no++){
allSelects[no].onfocus = ajax_options_hide;
}

var oldonkeydown=document.body.onkeydown;
if(typeof oldonkeydown!='function'){
document.body.onkeydown=ajax_option_keyNavigation;
}else{
document.body.onkeydown=function(){
oldonkeydown();
ajax_option_keyNavigation() ;}
}
var oldonresize=document.body.onresize;
if(typeof oldonresize!='function'){
document.body.onresize=function() {ajax_option_resize(inputObj); };
}else{
document.body.onresize=function(){oldonresize();
ajax_option_resize(inputObj) ;}
}
}

if(inputObj.value.length<minimumLettersBeforeLooku p){
ajax_options_hide();
return;
}
ajax_optionDiv.style.top = (ajax_getTopPos(inputObj) +
inputObj.offsetHeight + ajaxBox_offsetY) + 'px';
ajax_optionDiv.style.left = (ajax_getLeftPos(inputObj) +
ajaxBox_offsetX) + 'px';
if(ajax_optionDiv_iframe){
ajax_optionDiv_iframe.style.left = ajax_optionDiv.style.left;
ajax_optionDiv_iframe.style.top = ajax_optionDiv.style.top;
}

ajax_list_activeInput = inputObj;
ajax_optionDiv.onselectstart = ajax_list_cancelEvent;

if(ajax_list_cachedLists[paramToExternalFile][inputObj.value.toLowerCase()]){
ajax_option_list_buildList(inputObj.value,paramToE xternalFile);
}else{
ajax_optionDiv.innerHTML = '';
var ajaxIndex = ajax_list_objects.length;
ajax_list_objects[ajaxIndex] = new sack();
var url = ajax_list_externalFile + '?' + paramToExternalFile +
'=1&letters=' + inputObj.value.replace(" ","+");
ajax_list_objects[ajaxIndex].requestFile = url; // Specifying which
file to get
ajax_list_objects[ajaxIndex].onCompletion = function(){
ajax_option_list_showContent(ajaxIndex,inputObj,pa ramToExternalFile);
}; // Specify function that will be executed after file has been found
ajax_list_objects[ajaxIndex].runAJAX(); // Execute AJAX function
}
}

function ajax_option_keyNavigation(e)
{
if(document.all)e = event;

if(!ajax_optionDiv)return;
if(ajax_optionDiv.style.display=='none')return;

if(e.keyCode==38){ // Up arrow
if(!ajax_list_activeItem)return;
if(ajax_list_activeItem &&
!ajax_list_activeItem.previousSibling)return;
ajax_options_rollOverActiveItem(ajax_list_activeIt em.previousSibling,true);
}

if(e.keyCode==40){ // Down arrow
if(!ajax_list_activeItem){
ajax_options_rollOverActiveItem(ajax_list_optionDi vFirstItem,true);
}else{
if(!ajax_list_activeItem.nextSibling)return;
ajax_options_rollOverActiveItem(ajax_list_activeIt em.nextSibling,true);
}
}

if(e.keyCode==13 || e.keyCode==9){ // Enter key or tab key
if(ajax_list_activeItem &&
ajax_list_activeItem.className=='optionDivSelected ')ajax_option_setValue(false,ajax_list_activeItem) ;
if(e.keyCode==13)return false; else return true;
}
if(e.keyCode==27){ // Escape key
ajax_options_hide();
}
}

Sep 1 '06 #3
ja*******@gmail.com said the following on 9/1/2006 1:03 PM:
Randy Webb wrote:
>ja*******@gmail.com said the following on 9/1/2006 12:36 PM:
>>What I would like to do is have the user start typing in the last name
and as they were typing it would retrieve names from the database and
give a drop down like autosuggest in google. That much I have figured
out. But what I really want is once the name is in the field I would
like it to retrieve the rest of the information from the database ie.
firstname address etc. and put it other fields on the form. Is this
possible?
Yes.
Thanks Randy,

But how. I am able to do the autosuggest no problem and the fields do
fill in no problem either. But what is happening is when I use my
mouse to select from the autosuggest dropdown the other fields do not
update right away. I thought that if I use the onblur it would work.
In order for it to fill in the other fields I have to click on another
field and then come back to the autosuggest field and then click on
another field again. But if I don't use the mouse to select from the
dropdown it will work. What am I missing here.
Without the back-end for testing, I can only guess. But, I didn't see
anywhere in your ajax_showOptions function (anywhere in the code at all
actually) where you are setting focus back to the input after it
auto-fills. So the onblur for it never gets fired until you go to
another focus, come back, and then leave.

At the point where the user chooses an AJAX filled option, set focus to
the input. Then when you click out, or tab out, the focus will be lost
and fire the onblur event.

Input has focus
Autosuggest box updates, user clicks that box.
On that click, the input has lost focus.
Input gets filled.
You click another textbox.

The input didn't have focus to get the blur event triggered so you have
to focus it again then blur it.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 1 '06 #4
I seems so simple now. Thanks for that. I totally missed it. I
placed the setfocus in after the selection and it works like a charm.
Randy Webb wrote:
ja*******@gmail.com said the following on 9/1/2006 1:03 PM:
Randy Webb wrote:
ja*******@gmail.com said the following on 9/1/2006 12:36 PM:
What I would like to do is have the user start typing in the last name
and as they were typing it would retrieve names from the database and
give a drop down like autosuggest in google. That much I have figured
out. But what I really want is once the name is in the field I would
like it to retrieve the rest of the information from the database ie.
firstname address etc. and put it other fields on the form. Is this
possible?
Yes.
Thanks Randy,

But how. I am able to do the autosuggest no problem and the fields do
fill in no problem either. But what is happening is when I use my
mouse to select from the autosuggest dropdown the other fields do not
update right away. I thought that if I use the onblur it would work.
In order for it to fill in the other fields I have to click on another
field and then come back to the autosuggest field and then click on
another field again. But if I don't use the mouse to select from the
dropdown it will work. What am I missing here.

Without the back-end for testing, I can only guess. But, I didn't see
anywhere in your ajax_showOptions function (anywhere in the code at all
actually) where you are setting focus back to the input after it
auto-fills. So the onblur for it never gets fired until you go to
another focus, come back, and then leave.

At the point where the user chooses an AJAX filled option, set focus to
the input. Then when you click out, or tab out, the focus will be lost
and fire the onblur event.

Input has focus
Autosuggest box updates, user clicks that box.
On that click, the input has lost focus.
Input gets filled.
You click another textbox.

The input didn't have focus to get the blur event triggered so you have
to focus it again then blur it.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris Sharman | last post by:
I'd like to design my pages to work cooperatively with browser autofill features. I've loked around, but can't find any good documentation on supported/unsupported field names...
4
by: j.t.w | last post by:
Hi All. I'm having a problem with my Date of Birth textbox. When I open the ..htm file, the "DoB" textbox is flat with a border. All of my other textboxes are sunken and are yellow. When I...
0
by: David Portabella | last post by:
Hello, I am a doing a survey about Autofill Web Forms softwares. Usually, they all collect information of the user once during the set-up phase (user knowledge base). Then, when the user...
3
by: Anks | last post by:
Hi All, A user can control the AutoFill option by enabling / disabling the Remember Passwords option in Options - Privacy - Saved Passwords section. But is it possible to disable this option...
0
by: Ray Holtz | last post by:
Is it possible to autofill a field based on what is entered into another field in a form? My form has an employee field, and department field. In an Items Table, I have fields FldEmployee, and...
4
by: HTS | last post by:
I have written a membership database application (PHP + mySQL) and need to prevent autofill from filling in fields in a member record edit form. I know I can turn off autoFill in my browsers, I...
5
by: Sherry | last post by:
Is there a way to automatically, or by using say a function key, call/paste into a current table or query's field the same value entered for the same field in the immediately preceeding record,...
3
by: StuckAndNoClue | last post by:
I've searched through some of the answers on the forum regarding autofill but I can't work out how to do it for my database. I have two tables 1. Members info, containing the following fields: ...
1
by: Nik Coughlin | last post by:
I'm doing some ajax validation on form fields, using the form change event. If I click Autofill in the Google Toolbar, fields are filled out but the change event is never fired, so the validation...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.