How to realize this behaviour

hi,

i am just doing my first steps with autocompleter and php.

i want to know if it is possible to build the following behaviour:

i have a small test app with one textinput

Kontakt

the first problem the comes to me is, that there my be some entries
having the same caption (2 and 4) but different id in database.

— db ------
id, caption
1, hans
2, peter
3, paul
4, peter

so i would need a structure for autocomplete to retrieve database id and
a caption.

next problem then is where to store database id.

this could be done with ie a hidden field, but the how would the
database id of the selected element be stored in the hidden field.

i need the database id instead of the caption for storing in the
database, as i want to use my autocomplete field as a replacement for a
db lookup field.

next problem:

if the user enters a text in the input field and hits tab right behind,
then no lookup is done. how can i force lookup then.

the basic idea is this:

1.) user enters text in a autocomplete field
2.) if he leaves the field (ab or mouse click) a lookup has to be done
3.) if he has entered “han” then there is only one hit “hans”, so no
lookuplist is nessesary, text has only to be completed and record id
needs to be stored in form
4.) if he has entered “pe” then more then one record hits the search (2
and 4), so the lookuplist should appear to select the required item
5.) if no entry was found i would like to go to a create dialog to let
the user enter the missing lookup item

any hints welcome

thanks
stefan