Hey this script allows you to check if a value exist in a Database before form is submitted using Jquery
1: First create "form.php" or "form.html" and insert this code or your desired code
2: Next create a file which will validate the user inputted values with your database, here i called this file "check_user.php" but you can call it whatever you want
Working Demo Here: http://www.gtriddim.com/api/demo.php
Thats it
1: First create "form.php" or "form.html" and insert this code or your desired code
Code:
<html> <title>Check Username Demo</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> <script language="javascript"> $(document).ready(function() { $("#username").blur(function() { var username = $('#username').val(); if(username == '' || username.length < 0){ alert("Username is Required."); $('#username').removeClass("check"); } else{ $("#alert").removeClass().addClass('check').text('Checking If User Exist...').fadeIn("slow"); } $.post("check_user.php",{ username:$(this).val() } ,function(data) { if(data=='no') { $("#alert").fadeTo(200,0.1,function() { $(this).html('Username Already exists').addClass('error').fadeTo(900,1); }); } if(data=='yes') { $("#alert").fadeTo(200,0.1,function() { $(this).html('UserName available to register').addClass('ok').fadeTo(900,1); }); } }); }); }); </script> <style type="text/css"> .check{ position:absolute; width:auto; margin-left:30px; border:1px solid #c93; background:#ffc; padding:3px; } .ok{ position:absolute; width:auto; margin-left:30px; border:1px solid #349534; background:#C9FFCA; padding:3px; font-weight:bold; color:#008000; } .error{ position:absolute; width:auto; margin-left:30px; border:1px solid #CC0000; background:#F7CBCA; padding:3px; font-weight:bold; color:#CC0000; } </style> <br/><br/><br/><center>Check if username exist in database using <b>Jquery</b></center><br/><br/> <center><b>NOTE:</b> Just enter any random name to check if the name exist<br/><br/><br/></center> <center>Registered name : <b>Anthony</b><br/></center> <center><p><h2>UserName:</h2> <input name="username" type="text" id="username" style="padding:2px; border:4px solid #ADD8E6; width:280px; height:44px; font-family:Verdana, Arial, Helvetica, sans-serif;font-size:14px;"><span id="alert" style="display:none"></span></p><br/><br/> <input name="" type="submit" value="Click Here" style="padding:2px; border:1px solid #FF0000; width:180px; height:24px; font-family:Verdana, Arial, Helvetica, sans-serif;font-size:14px;"><br/><br/><br/></center> <center>Demo Provided By: <a href="http://www.gtriddim.com" target="_blank">www.gtriddim.com</a><br/><br/></center> </html>
2: Next create a file which will validate the user inputted values with your database, here i called this file "check_user.php" but you can call it whatever you want
HTML Code:
<?php include ('config.php'); $user_name=htmlspecialchars($_POST['username'],ENT_QUOTES); $rs_duplicates = mysql_query("select username from users where username ='$user_name'"); $duplicates = mysql_num_rows($rs_duplicates); if($user_name == '') { echo "UserName is Required"; exit(); } if ($duplicates > 0) { echo "no"; } else if ($duplicates < 1) { echo "yes"; } ?>
Working Demo Here: http://www.gtriddim.com/api/demo.php
Thats it
Comment