php - Check the username availability and update the fields and also the username without ajax and jquery -
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <?php include('db_connection.php'); error_reporting(e_all); session_start(); ?> <html> <head> <title>register form</title> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript"> function validate(form) { var name = document.register.name.value; var email = document.register.email.value; var apos = document.register.email.value.indexof("@"); var dotpos = document.register.email.value.lastindexof("."); var username = document.register.username.value; var password = document.register.password.value; var con_password = document.register.con_password.value; if (name==null || name=="") { alert("enter name"); document.register.name.focus(); return false; } else if (email==null || email=="") { alert("enter email"); document.register.name.focus(); return false; } else if ( apos<1 || dotpos-apos<2) { alert("invalid email address!!!") document.register.email.focus(); return false } else if (username==null || username=="") { alert("enter user name"); document.register.username.focus(); return false; } else if (password.length < 6 || password.length > 12 ) { alert("password must contain @ least 6 less 12 characters!"); document.register.password.focus(); return false; } else if (con_password.length < 6 || con_password.length > 12) { alert("password must contain @ least 6 less 12 characters!"); document.register.con_password.focus(); return false; } else if (password != con_password) { alert("you did not enter same new password twice.please re-enter password!"); document.register.con_password.focus(); return false; } else { return true; } } </script> </head> <body> <?php if(isset($_session['session_name1'])) { unset($_session['session_name1']); unset($_session['session_email1']); unset($_session['session_username1']); } if(isset($_get['action'])) { $action=$_get['action']; } else { $action = 'add'; } if($action=='edit' && $_get['id']!="") { $id = $_get['id']; $user_edit = mysql_query("select name,email,username,password,date loginform userid='$id'"); $row_edit = mysql_fetch_array($user_edit); $edit_name = $row_edit['name']; $edit_email = $row_edit['email']; $edit_username = $row_edit['username']; $edit_password = $row_edit['password']; } if(isset($_post['submit'])) { $name = $_post['name']; $email = $_post['email']; $username = $_post['username']; $password = $_post['password']; if($action=='add') { $query = "select username loginform username = '$username'"; $result = mysql_query($query) or die (); $check_username = mysql_num_rows($result); if($check_username ==0) { mysql_query("insert loginform (userid,name,email,username,password,date)values('','$name','$email','$username','$password',now()) ") or die(mysql_error()); header("location:login.php"); } else {?> <center><b><?php echo "ohhh....user name taken";?></b></center> <?php } } if($action=='edit') { $query = "select userid,username loginform username = '$username'"; $result = mysql_query($query) or die (); $check_username = mysql_num_rows($result); echo "=====>".$check_username; if($check_username ==0) { mysql_query("update loginform set name='$name',email='$email',username='$username',password='$password' userid='$id'") or die(mysql_error()); header("location:list_view.php"); } else { echo "<b>ohhh....user name taken</b>"; //mysql_query("update loginform set name='$name',email='$email',password='$password' userid='$id'") or die(mysql_error()); //header("location:list_view.php"); } } } ?> <form name="register" action="" method="post"> <table align="center" border="1" cellpadding="2" cellspacing="1"> <tr> <td colspan="2" align="center"><?php if(!empty($_get['id'])) { ?><h2>update form</h2><?php } else { ?><h2>register form</h2><?php }?></td> </tr> <tr> <td>name:</td> <td><input type="text" name="name" id="name" value="<?php if($action=='edit') echo $edit_name; ?>"/></td> </tr> <tr> <td>email:</td> <td><input type="text" name="email" id="email" value="<?php if($action=='edit') echo $edit_email; ?>"/></td> </tr> <tr> <td>user name:</td> <td><input type="text" name="username" id="username" value="<?php if($action=='edit') echo $edit_username; ?>"/></td> </tr> <tr> <td>password:</td> <td><input type="password" name="password" id="password" value="<?php if($action=='edit') echo $edit_password; ?>"/></td> </tr> <tr> <td>confirm password:</td> <td><input type="password" name="con_password" id="con_password" value="<?php if($action=='edit') echo $edit_password; ?>"/></td> </tr> <tr> <td colspan="2" align="center"><?php if(!empty($_get['id'])) { ?> <input type="submit" value="update" title="update" name="submit" onclick="return validate(this.form);"/> <?php } else { ?> <input type="submit" name="submit" title="add" value="submit" onclick="return validate(this.form);"/> <?php }?> </td> </tr> </table> </form> <?php ?> </body> </html>
.......................... dai use query , whether exist or not,
$username=$_post['usernaem']; $admin_detail=admincontrollers->getadmindetail($username); //ajax file //queryy select * table_name username ='$username'; return $result; if(isset($result) && count($result)>0 ) echo "var isexist = 'yes'"; else echo "var isexist = 'no'" //end of ajax note:
select * table_name username ='$username'; above query key find isexist or not ................
Comments
Post a Comment