I'm creating a registration form. The user enters the username and password, and presses submit, and the form is submitted using POST. HTML :
<link href="Styles/RegisterStyles.css" rel="stylesheet" type="text/css" />
<form id="frmRegister" method="post" action="register.php">
<h1>Register</h1>
<table width="100%">
<tr>
<td width="16%"><label class="alignRight"> Username: </label></td>
<td width="84%"><input name="txtUsername" type="text" maxlength="40" /></td>
</tr>
<tr>
<td width="16%"><label class="alignRight"> Password: </label></td>
<td width="84%"><input name="txtPassword" type="text" maxlength="40" /></td>
</tr>
<tr>
<td width="16%"> </td>
<td width="84%"><input name="Submit" class="submitButton" type="submit" /></td>
</tr>
</table>
</form>
</html>
PHP:
$username = $_POST["txtUsername"];
$password = $_POST["txtPassword"];
//Code to connect to database
function doesUsernameExist($username)
{
//return true if username exists or false otherwise
}
Now, in PHP, I run a query to check if the username exists in the database. If the username already exists, how can I notify the user without navigating to another page and causing the "username" and "password" fields to be reset to blank?
Some registration forms have a really neat Javascript that checks if the username exists each time you press a key on the keyboard. Any ideas on how this could be implemented? It's difficult ( and bad practice ) to connect to a database using JavaScript from what I can gather.