检查用户名可用性并更新字段以及没有ajax和jquery的用户名

时间:2011-04-30 04:39:13

标签: php

<!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 The Name");
                 document.register.name.focus();
                 return false;
              }
             else if (email==null || email=="")
              {
                 alert("Enter The 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 The User Name");
                 document.register.username.focus();
                 return false;
              }
             else if (password.length < 6 || password.length > 12 ) 
              { 
                    alert("Password must contain at least 6 less than 12 characters!"); 
                    document.register.password.focus();
                    return false; 
              } 
             else if (con_password.length < 6 || con_password.length > 12)
              {
                 alert("Password must contain at least 6 less than 12 characters!"); 
                 document.register.con_password.focus();
                 return false;
              }
             else if (password != con_password)
              {
                 alert("You did not enter the same new password twice.Please re-enter your 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 FROM loginform WHERE 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 FROM loginform WHERE UserName = '$username'";
            $result         = mysql_query($query) or die ();
            $check_username = mysql_num_rows($result);
            if($check_username ==0)
            {
                mysql_query("insert into 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 Already Taken";?></b></center>
      <?php }
        }
        if($action=='edit')
        {
            $query          = "SELECT UserId,UserName FROM loginform WHERE 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' WHERE UserId='$id'") or die(mysql_error());
                header("Location:list_view.php");
            }
            else
            {
                echo "<b>Ohhh....User Name Already Taken</b>";
                //mysql_query("UPDATE loginform SET Name='$name',Email='$email',Password='$password' WHERE 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>

1 个答案:

答案 0 :(得分:1)

.......................... dai使用查询并获取它是否存在,

$username=$_POST['usernaem'];
$admin_detail=Admincontrollers->getadmindetail($username);
//ajax file
//queryy
select * from table_name where username ='$username';
return $result;

if(isset($result) && count($result)>0 )
echo "var Isexist = 'yes'";
else
echo "var Isexist = 'no'"
//End of ajax

注意:

select * from table_name where username ='$username';

以上查询是查找IsExist的关键 ................