to add the popup alert you'll need to include javascript, which i've got no clue about, to your code, also the php validation has to be in the same file as the mainpage
<?php
// Validation goes here
if (OK) { javacode }
elseif (FAILED) { javacode }
?>
-----
main form stuff goes here
as for the MYSQL you could do this
function DbConnect() {
mysql_connect('host','user','pass');
mysql_select_db('dbname');
}
function IsUser($username) {
$sql = "SELECT `username` FROM `contacts` WHERE `username`='".$username."'";
$result = mysql_query($sql);
$match = mysql_num_rows($result);
return $match;
}
$userExists = IsUser($username);
if ($userExists > 0) {
// Tell user to find another username
} else {
// Do all the rest of the registration here
}
this is really quick written, no testing or anything, but it'll guide you in the right direction i hope