Can I have javascript and php work together to check if an entry already exists in the database?

I have a form that submits new data into a database, but I would like the page to notify the user if their entry is already entered before they click submit. For example if they type in a certain username into the form, can javascript tell them that the user name already exists before clicking submit? If so, how do you do it? Thanks

Anonymous2010-06-23T00:59:22Z

Favorite Answer

Yes, it's possible. Look into the Ajax (Asynchronous JavaScript And XML) technology.

earline2016-06-04T03:22:10Z

When the database isn't finding the '$name' value in your field it is returning 0 rows. I bet your php code assumes that you are getting at least one row back. Hence the error when the resource doesn't have anything in it. There are two ways around it. Either you can modify your sql to $query = "SELECT count(*) FROM users WHERE name='$name';"; which will guarantee you a result where you can just check that it's greater than 0 or you can use the mysql_num_rows() function to check that you actually have a row before you fetch the first row e.g. // does user exist? $name = mysql_real_escape_string($name); $query = "SELECT * FROM users WHERE name='$name';"; $res = mysql_query($query); if (mysql_num_rows($res) > 0) { // yes, pull in the user details } else // no, user doesn't exist }