I don't know what the question is but I will recommend trying out jQuery for beginning AJAX... there are many functions that make it a breeze and take out all the advanced coding involved with doing it from scratch. Check it out:
Depending a bit on the browser you use the onkeypress
event will also fire on keys that do not change the content like the arrow keys, the enter or tab key, F5 to refresh, etc... With some browsers even pressing shift might trigger onkeypress
. See this page for details.
As your PHP code does not check for the entered value being new it will add the entered value every time you press one of these keys.
Furthermore it is a good idea to use jQuery for this.
onkeypress will fire for EVERY keypress even you do within that box. So here's what happens:
- You type 'j'
- showHint() is triggered, sends 'j' to the server
- The script inserts 'j' into your user table
- You type 'o', there is now 'jo' in the text field
- showHint() is triggered, sends 'jo' to the server
- The script insert 'jo' into your user table
- etc...
In other words, you're not showing a hint at all, you're just blindly inserting whatever the user types into your database.
If you want to show hints, then you should be doing at LEAST a 'SELECT' query instead and returning the results to your page.
You should also use something like Mootools or jQuery to do your AJAX calls. They'll handle the hard parts of building/sending the request for you, without having to worry about what browser the user's using.
As well, read about about SQL injection before releasing a script such as yours out into the wild.
You also have a mal-formed insert query. To insert a new record, the basic syntax is:
INSERT INTO sometable (field1, field2, field3, ...) VALUES (value1, value2, value3, ...)
You've got mixed in some sort of partial 'update' query in there, the format of which is
UPDATE sometable SET field1=value1, field2=value2, ....
I can't see how your query could be inserting anything into the database as it is now, as the syntax is completely broken.
Among the many different issues with your code, maybe you should use onblur? If you are hinting like tooltips then maybe on mouseover but why are you inserting rows?