我试图使用带有post方法的AJAX将用户名和密码发送到php文件。如何检索字段

时间:2014-03-23 05:28:44

标签: php html ajax

如何访问这些变量? 我试图在php文件中检索用户名和密码,但是,如果我使用$_POST['Username'}

,它会显示未定义的索引用户名
 <script>
    var a = new XMLHttpRequest();
    a.onreadystatechange = function(){
         if (a.readyState==4 && a.status==200) {
              ajaxFinished = true; 
              alert(a.responseText);
         }
         else {
         }                          
    }
    a.open("post","php/psswd.php",true);
    a.send('"Username="+document.getElementByNames("Username")+"&Password="+getElementByNames("Password")'); // posting username and password
 </script>

如何在php文件中检索这些字段?

3 个答案:

答案 0 :(得分:1)

我自己找到了答案,问题是,    a.setRequestHeader(&#34;内容类型&#34;&#34;应用程序/ X WWW的窗体-urlencoded&#34); 需要添加。而document.getElementsByName(&#39; xyz&#39;)返回nodeList,但不返回特定节点,我们需要遍历那个nodeList。

答案 1 :(得分:0)

语法为getElementsByName,而不是您现在的getElementByNames

单词Elements是复数,而不是Names。

<script>
    var a = new XMLHttpRequest();
    a.onreadystatechange = function(){
        if (a.readyState==4 && a.status==200)
            {
            ajaxFinished = true; 
            alert(a.responseText);
            }
        else
            {

            }

        }
        a.open("post","php/psswd.php",true);
        a.send('"Username="+document.getElementsByName("Username")+"&Password="+getElementsByName("Password")'); // posting username and password
</script>

有关此内容的更多信息,请访问:

https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByName


修改

以下使用jQuery并使用FF 28.0和IE 7进行测试。

旁注:您可能想要更改此window.location = "success.php"

<!DOCTYPE html>

<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

<script>
function chk_ajax_login_with_php(){
  var username=document.getElementById("username").value;
  var password=document.getElementById("password").value;

    var params = "username="+username+"&password="+password;
    var url = "php/psswd.php";
        $.ajax({
           type: 'POST',
           url: url,
           dataType: 'html',
           data: params,
           beforeSend: function() {
             document.getElementById("status").innerHTML= 'checking...'  ;
                     },
           complete: function() {

           },
           success: function(html) {

                 document.getElementById("status").innerHTML= html;
                 if(html=="success"){

                   window.location = "success.php"

                 }

            }
   });

}
</script>


</head>


<body>
<div id='logindiv'>

    <label>Username:</label>
        <input name="username" id="username" type="text">
    <label>Password:</label>
        <input name="password" id="password" type="password">
        <input value="Submit" name="submit" class="submit" type="submit" onclick='chk_ajax_login_with_php();'>
    <div id='status'></div>
</div>

答案 2 :(得分:0)

而不是使用XMLHttpRequest方法,请看一下:

<script type="text/javascript">
    $('#loginForm').submit(function(e) {
            var username    = $("#login-username").val(); //id of the form text input
                password    = $("#login-password").val(); //id of the form text input

             e.preventDefault();
         $.ajax({
             type: "POST",
             url: url,
             data: { form: 'login', username: '' + username + '', password: '' + password + ''}
           }).success(function( msg ) {
             //msg is the text returned from the PHP function that will validate the login
               $('.login_success_text').html(msg);
          });
        });
</script>

<body>

               <form role="form" name="loginForm" id="loginForm" method="POST">
                  <label>Username</label>
                  <input id="login-username" class="form-control text placeholder" placeholder="Username" name="username" type="text">
                  <label>Password</label>
                  <input id="login-password" class="form-control password placeholder" placeholder="Password" name="password" autocomplete="off" type="password">
                <input type="submit" value="Login" />
                <div class="login_success">
                  <span class="login_success_text"></span>
                </div>
</body>