Forum Moderators: open

Message Too Old, No Replies

How can I check for a record in a db with onFocus?

Javascript onFocus

         

slimcharlie

10:05 pm on Oct 12, 2004 (gmt 0)

10+ Year Member



Hi everyone, I am new to every programming language and everything I've learned so far has been taken from the web. I'd really appreciate if someone could help me with articles or code or the following.
I have 2 fields in a form. When the first field is populated with 5 caracters the second field gets focus automatically. Now what I need to do is use the onFocus event to grab the value on the first field and check it against a mysql db. I do not want to use buttons to do this.
All I need is the system to alert the user if this value exists in the db, if it does then let the user continue with the rest of the form.
Thanks a lot...

Sanenet

10:20 pm on Oct 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi slimcharlie, and welcome to webmasterworld.

In order to do this, you have two options:

If the list of values in the db are not very many, you could load them all up into a javascript array, and check the value of the first box against that array. (You don't want to do this if you have too many values, or if the values keep changing, etc).

Otherwise, and possible the easier way, upon filling in the first field, the form submits itself to a cgi script (PHP, ASP, PERL, or whatever your site is running), which checks the value against the db and returns a status message to the user (either try again, or value exists).

As to the code, it depends on your exacts needs, and the serverside code you're running. Try out a few resources likes dynamicdrive.com or javascript.internet.com for the JS.

Best of luck!

killroy

10:57 pm on Oct 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is a third alternative, which is both, the most complex, as well as teh client side most ellegant one.

Use onFocus to execute javascript which does a server query for the data in question by loadign a page into a hidden iframe. Use onload on that page to activate te desired action according to teh result of the DB lookup, such as popping up a notification or populating the second list box as you like.

SN

Sanenet

11:03 pm on Oct 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



..unless the user is running an older browser that doesn't support Iframe...