簡體   English   中英

使用JavaScript驗證密碼

[英]Validation of password using javascript

以下是username.php的代碼

    <html>
  <head>
       <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
      <!-- <script type="text/javascript" src="js/jquery-ui-1.8.17.custom.min.js"></script> -->

       <script type="text/javascript">

         $(document).ready(function(){
            $("#username").change(function(){
                 $("#message").html("<img src='ajax-loader.gif' /> checking...");


            var username=$("#username").val();

              $.ajax({
                    type:"post",
                    url:"check.php",
                    data:"username="+username,
                        success:function(data){
                        if(data==0){
                            $("#message").html("Username available");
                        }
                        else{
                            $("#message").html("Username already taken");
                        }
                    }
                 });

            });

         });

       </script>
  </head>

  <body>

       <table>
        <tr>
              <td>Username</td>
              <td>:</td>
              <td><input type="text" name="id" id="username""/><td>
                <td id="message"><td>
        </tr>

        <tr>
              <td>Password</td>
              <td>:</td>
              <td><input type="text" name="password" id="password" /><td>
        </tr>
       </table>
  </body>
</html>

以及check.php的代碼

    <?php

  mysql_connect("localhost","healhmate","healthmate");
  mysql_select_db("hmdb");
  if(isset($_POST['id']))
  $username=$_POST['id'];
  $query=mysql_query("SELECT * from user where id='$username' ");

  $find=mysql_num_rows($query);

  echo $find;

?>

此代碼將輸出作為用戶名和密碼框。 我將所有3個文件ajax-loader.gif,username.php和check.php都包含在一個文件夾中。在輸入用戶名時不執行任何驗證。 誰能幫我弄清楚為什么會這樣嗎?

 <?php

  mysql_connect("localhost","healhmate","healthmate");
  mysql_select_db("hmdb");
  if(isset($_POST['username']))// because in ajax you send username not id
  $username=$_POST['username'];
  $query=mysql_query("SELECT * from user where id='$username' ");

  $find=mysql_num_rows($query);

  echo $find;

?>

在Ajax請求中使用data:"id="+username ,因為這是您在PHP中檢查的POST變量。

另外請注意:確保處理未設置$ _POST ['username']的情況。

<?php
mysql_connect("localhost","healhmate","healthmate");
mysql_select_db("hmdb");
if(isset($_POST['username'])) {// because in ajax you send username not id
    $username=$_POST['username'];
    $query=mysql_query("SELECT * from user where id='$username' ");
    $find=mysql_num_rows($query);
   echo $find;
} else {
    echo "-1";
}
?>

並且不要使用mysql_ *函數。 他們已棄用。 使用mysqli_ *函數或PDO。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM