09-14-2009, 09:47 PM
|
#5 (permalink)
|
|
The Addict
Join Date: May 2009
Posts: 287
Thanks: 5
|
Just use JS/jQuery to access a .php file (with an attribute of ?user={username}) and then have it return the string (E.g. <span style="color:#009900;">Username is not taken!</span>) and place that in the elements .innerHTML property.
HTML Code:
<form action="" method="">
<input type="text" name="username" id="username_elm" />
<button onclick="check_username('username_elm', document.getElementById('username_elm').value);">Check Username</button>
</form>
javascript Code:
function check_username(elm, value) { var xhr = new XMLHttpRequest(); xhr.open('GET', '/path/to/script.php?username=' + value, false); xhr.send(null);
document.getElementById(elm).innerHTML = xhr.responseText; return; }
PHP Code:
<?php
$link = mysql_connect(/* Arguments... */);
$sql = 'SELECT username FROM db.users WHERE username = ' . mysql_real_escape_string($_GET['username']) . ' LIMIT 1');
$result = mysql_query($sql, $link);
if (mysql_num_rows($result) == 0) { echo '<span style=\'color:#009900;\'>Username is not taken!</span>'; } else { echo '<span style=\'color:#990000;\'>Username is taken.</span>'; } ?>
(Wow, I wrote more than I thought I was going to...)
Notes: - You would want to store the JS DOM value for the element in a variable.
- You would need to check for w3c browser compatibility with the XMLHttpRequest (xhr).
- You could just pass the DOM element in the JS function (faster).
|
|
|
|