hi guys,

i have registration form, where the user enters username, email id etc. on clicking register, the data should get validated and then get saved into the database. i got most of the validation stuff right exept this one. i did all the validations in javascript, i want the username to be unique, so what i did is, after posting all the data to php, i fetched every username from the table and compared it, when they are same echoed the javascript alert function. is this the right way of doing it??
or
can we compare the username before posting it to the php code(i mean in the javascript at the beginning of code in <head></head>), if so how do i fetch the username from table in javascript. pls explain with a example, so i get to know the syntax as well..

thank u..

Member Avatar for diafol

Yes, this is a common technique for avoiding frustrating your registering users. Nothing worse than filling a few fields and then getting a 'ha ha - username already taken' message. Use ajax to do a DB search when the username form field loses focus. Have an empty <span id="usermsg"> or something similar next to the field. Get Ajax to place a 'green tick' if OK, a 'red cross' with a simple message if the username is already taken. I suggest using jQuery for Ajax.

I ain't gonna give an example - that's up to you. Although here's a rough guide:

1. onblur attribute - bind to a js function, e.g. onblur="checkUsers();return false;"
2. js function in an external file (or script in head area) gets the username from the form field
3. The value is passed to a php script (e.g. as a post parameter)
4. The php script checks the DB and returns the result, e.g. mysql_num_rows($result) is an easy way - 0 = available, 1 = unavailable. Just use

echo mysql_num_rows($result);

5. The js script receives this as an Ajax response. Set the usermsg span content accordingly.

hey thanks to both of u, for the hints and not providing the examples, thus putting my brain to more work :D..since im beginner in php, i have followed the 1st 4 steps mentioned by ardav, but stuck on th 5th step so going thru the pages of w3schools to get the hang of what ajax and jquery are. i hope i ll be able to figure it out..

hi..

i tried the following code to check if the user already exists or not.

<td>User Name:</td><td> <input type="text" name="username" id="username" onblur="checkUser();"/>*</td>
<?php
$username=$_POST['username'];
$link = mysql_connect("xxx","xxx","xxx");
mysql_select_db("xxx",$link);
$sql="SELECT * FROM users WHERE username='$username'";
$res=mysql_query($sql);
$row=mysql_fetch_array($res);
while($username==$row['username'])
{ 
     echo"<script type='text/javascript'>";
     echo"function checkUser(){alert('User already exists, please enter different name');}";
	echo"</script>";
}
?>

browser just hangs the process doesnt complete at all..
any idea where i am going wrong or how do i fix this..
i have also tried this

<td>User Name:</td><td> <input type="text" name="username" id="username" onblur="checkUser();"/>*</td>
<?php
$username=$_POST['username'];
$link = mysql_connect("xxx","xxx","xxx");
mysql_select_db("xxx",$link);
$sql="SELECT * FROM users WHERE username='$username'";
if(mysql_query($sql))/*according to me this will be true only when name entered by user already exists in table*/
{ 
     echo"<script type='text/javascript'>";
     echo"function checkUser(){alert('User already exists, please enter different name');}";
	echo"</script>";
}
?>

for this one it always returns user already exists, even if the user doesnt exist..

Member Avatar for diafol

That won't work at all. You need an onblur attribute. I'll place this inline for now, although you can get a little more unobtrusive with it if you want.

<form ...>
...
<input id="username" name="username" onblur="doNameCheck();return false;" /> <span id="username_reply"></span>

...
</form>

In a js file, have your doNameCheck function. Remember to link to this external js file in the HEAD area, e.g.

<script src="myfile.js"></script>
function doNameCheck(){
 //get the username value from the form - I'd use jQuery
 //make the value a post parameter passed to your namechecker php script
 //receive your answer and update the span (username_reply) with info depending on a return value of 0 or 1
}

Your namechecker php script does all the hard work:
pick up the value via $_POST variable
checks your value against the DB
returns a 1 for 'being used', return a 0 for 'available'

job done!

**Very important that you sanitize all input - you can protect yourself from XSS and SQL injection with mysql_real_escape_string(htmlentities($_POST[...])) or similar.**

In addition, you WILL need server-side validation as anybody can mess with js - it's not secure. This is only required when the form is actually submitted.

Member Avatar for P0lT10n

dont forget to use mysql_real_escape_string() for preventing sql injections...

thank u guys..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.