Reputation: 1
OK I'm having an issue with this too - I have a mysql db and PHP pages. I'm submitting a form to the database and it already checks for empty strings and highlights fields if there are errors etc.
On one of the fields though (specifically the username) I need to check if it already exists in Col_1 of the DB and if TRUE then throw an error alert without submitting the form and if FALSE go through the form submission that is already validating.
So the real question I have... (maybe m3tsys too)... is really this:
Is there a way to onBlur/onChange/onSubmit (all of which can stop the form from submitting data to the DB) my form field to check if exists in db then throw alert?
or,
Is there another, perhaps easier way anyone can think of? I know I'm making this more complicated the more I keep thinking about it, sorry.
Upvotes: 0
Views: 1405
Reputation: 77
If I don't know Ajax, I would do something like that I would check if that value exists or not. If it exists I manually generate a situation where I get an error for php e.g $error = 'have duplicate Key $your_column'; then just echo where you wanna show that error.
Second way that I have came to know that in duplicate key, php generates an error called
on duplicate key
and I would catch that and showing something as I have mentioned above. But as of now, I don't know how I can catch that error. Perhaps mysqli_error() can be useful here :)
Update
Just check before if column has any records and If it does if record found { user already exits } else { its a new user }
Upvotes: 0
Reputation: 324600
Use AJAX.
On changing the username field, call a PHP script that checks if a username exists. If it exists, show an error on the page. If not, it's available.
Upvotes: 2