I have a form to filled out by user. Its user registration form. I want that when user fill username text,my form should search for user name whether it exist or not.If it exist then username text field should get focus.My database table name is users and it contains column named as id,username,password,status. Here is my code.
<form name="user" action="#" method="post">
<label>Username</label>
<input type="text" maxlength="25" name="username" />
/*here i want to check from my db table weather username exists or not*/
<label>Password</label>
<input type="password" maxlength="25" name="password" />
<input type="submit" value="Submit" />
</form>
I can do it when user submits the form.But my requirement is to check value with out submitting form and username should get focus. and in php I have following code
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$username = $_REQUEST['username'];
$query=mysql_query("SELECT * from user where name='$username'");
$row=mysql_num_rows($query);
if($row==1){
echo "true";
} else {
echo "false";
}
?>
I have a form to filled out by user. Its user registration form. I want that when user fill username text,my form should search for user name whether it exist or not.If it exist then username text field should get focus.My database table name is users and it contains column named as id,username,password,status. Here is my code.
<form name="user" action="#" method="post">
<label>Username</label>
<input type="text" maxlength="25" name="username" />
/*here i want to check from my db table weather username exists or not*/
<label>Password</label>
<input type="password" maxlength="25" name="password" />
<input type="submit" value="Submit" />
</form>
I can do it when user submits the form.But my requirement is to check value with out submitting form and username should get focus. and in php I have following code
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$username = $_REQUEST['username'];
$query=mysql_query("SELECT * from user where name='$username'");
$row=mysql_num_rows($query);
if($row==1){
echo "true";
} else {
echo "false";
}
?>
Share
Improve this question
edited Feb 27, 2018 at 12:03
user8280225
asked Dec 4, 2012 at 10:04
Naeem ShakirNaeem Shakir
571 gold badge1 silver badge12 bronze badges
2
- 5 You need to use ajax for this – Dr. Dan Commented Dec 4, 2012 at 10:05
- this might help blog.webwizo./2011/06/03/… – danish hashmi Commented Dec 4, 2012 at 10:07
5 Answers
Reset to default 3Make an ajax request to a server side script of yours that does the check on the database and returns something you can intepret. Then act on it on the ajax success response (focusing the field).
There are thousands of examples of this online. Check @danish hashmi 's example ( http://blog.webwizo./2011/06/03/check-username-availability-in-php-with-jquery-ajax/) or google it for more.
Html Form
<form method="post" action="register.php">
......
<label for="username">Username</label>
<input type="text" maxlength="25" name="username" id="username">
......
</form>
Js
<script src="jquery-1.8.0.min.js"></script>
<script>
$(document).ready(function(){
$('#username').keyup(check_username); //use keyup,blur, or change
});
function check_username(){
var username = $('#username').val();
jQuery.ajax({
type: 'POST',
url: 'check_username.php',
data: 'username='+ username,
cache: false,
success: function(response){
if(response == 0){
alert('available')
}
else {
alert('not available')
//do perform other actions like displaying error messages etc.,
}
}
});
}
</script>
write your table user name checking in check_username.php
, the response from this php is count of rows with the provided username,something like below
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'username';
/*** mysql password ***/
$password = 'password';
/*** mysql databse ***/
$dbname = 'database';
try
{
$dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
/*** echo a message saying we have connected ***/
//echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$stmt = $dbh->prepare("SELECT * FROM user where name = ?");
$stmt->execute(array($_POST['username']));
echo $stmt->rowCount();
?>
I'm thinking of you binding checking for existence of user on keyup event of your name="username" field (should give an id to that). do you use jquery or any other library for js?
It would be much simpler to achieve what you're planning to do with using libraries.
To do so you can set textbox on change event and pass textbox value to ajax call like below.
input type="text" id="username"
Now in js file add below code.
$(document).ready(function(){
$(#username).change(function(e)
{
e.preventDefault();
$.ajax({
url : 'youphpfile.php?username='+$(#username).val();,
//or $(#username).attr('value')
dataType: 'json',
success : function()
{}
});
});
});
now in youphpfile.php file do checking for sended username parameter.
$username = $_REQUEST['username']; // $_GET['username']; get will be good for faster response.
mysql_connect
and check username
And you done. Cheers.
You will probably need a web service to check if the username exists and when the username field loses focus, make an ajax call to the service using jQuery.
$(document).ready(function(){
$('input[name="username"]').blur(function(){
var txt = $(this);
var username = txt.val();
$.ajax({
type: "POST",
url: "myUsernameService.php",
data: { uname: username }
}).done(function(res) {
if (res == "false"){
txt.removeClass("valid").addClass("invalid");
} else {
txt.removeClass("invalid").addClass("valid");
}
});
});
});
I wouldn't suggest focusing on the username textbox again as the AJAX call can take a couple of seconds to return a value, at which point the user might be busy typing in another field.
The above code assumes that you have a method on "myUsernameService.php" that accepts one variable called "uname" and returns a string of "true" or "false".