为什么使用ajax通过输入`type =“password”`发送数据不起作用?

时间:2014-06-05 08:37:17

标签: php jquery ajax passwords

为什么使用输入type="password"的ajax发送数据不起作用?

主要想法当我填写密码少于6个字符时,它将显示Password minimum 6 characters

好的,当我测试此代码时它不起作用,然后我从

编辑代码index.php

<input type="password" name="password" id="password"

<input type="text" name="password" id="password"

工作正常。

我的问题是,如何将此代码应用于<input type="password" name="password" id="password"

的index.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script language="JavaScript">
       var HttPRequest = false;

       function doCallAjaxpassword() {
          HttPRequest = false;
          if (window.XMLHttpRequest) { // Mozilla, Safari,...
             HttPRequest = new XMLHttpRequest();
             if (HttPRequest.overrideMimeType) {
                HttPRequest.overrideMimeType('text/html');
             }
          } else if (window.ActiveXObject) { // IE
             try {
                HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
             } catch (e) {
                try {
                   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
             }
          } 

          if (!HttPRequest) {
             alert('Cannot create XMLHTTP instance');
             return false;
          }

          var url = 'check_password_lenght.php';
          var pmeters = "CPassword=" + encodeURIComponent( document.getElementById("password").value);

            HttPRequest.open('POST',url,true);

            HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            HttPRequest.setRequestHeader("Content-length", pmeters.length);
            HttPRequest.setRequestHeader("Connection", "close");
            HttPRequest.send(pmeters);


            HttPRequest.onreadystatechange = function()
            {

                if(HttPRequest.readyState == 3)  // Loading Request
                {
                    document.getElementById("mySpanpassword").innerHTML = "..";
                }

                if(HttPRequest.readyState == 4) // Return Request
                {
                    if(HttPRequest.responseText == 'Y')
                    {
                        window.location = 'AjaxPHPRegister3.php';
                    }
                    else
                    {
                        document.getElementById("mySpanpassword").innerHTML = HttPRequest.responseText;
                    }
                }

            }

       }
    </script>








        <input type="password" name="password" id="password" onchange="JavaScript:doCallAjaxpassword();" value="<?php echo htmlspecialchars($_POST['password']); ?>"> <span id="mySpanpassword"></span>

check_password_lenght.php

<?php
$strPassword = $_POST[CPassword];
            // ฟังก์ชันตรวจสอบว่า password มีความยาวเกิน 6 ตัวหรือไม่
            $strlen_password =  strlen($strPassword);
            if ( $strlen_password < 6 ) 
                {
                    echo "Password minimum 6 characters";
                } 
?>

1 个答案:

答案 0 :(得分:0)

最好不要在页面上加载jQuery时使用XMLHttpRequest对象,这样浪费时间。

HTML

<input type="password" name="password" id="password"/>

JS

$.ajax({
    type: "POST",
    url: "check_password_lenght.php",
    data: { password : $('#password').val() }
}).done(function( response ) {
    alert( response );
});

根据需要进行编辑。