0

这段代码有什么问题?尝试通过带有 javascript 的 POST 将数据发送到 PHP 页面,但它没有发送任何内容,标题中没有任何内容 $_POST 包含任何内容。

HTML:

<form method="POST" id="userSub" class="userSub">
<table>
    <tr><td colspan="2">Sign In Here&nbsp;</td></tr>
    <tr><td>Username:</td><td><input name="username" type="text" id="username"  /></td></tr>
    <tr><td>Password:</td><td><input name="pwd" type="text" id="pwd" /></td></tr>
    <tr><td><input name="submit" type="submit" value="submit" id="submit" onclick="loginSub()" /></td></tr>
    <tr><td colspan="2">Need a Username? <a href="signup.html">Sign Up</a></td></tr>
</table>
</form>

Javascript:

function loginSub(){
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("rssnav2").innerHTML=xmlhttp.responseText;
    }
  }

xmlhttp.open("POST","PHP/login.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send();

}

PHP 现在没有做任何特别的事情,只是看看我是否可以将帖子信息传递给它:

<?php
echo "test";
$username=$_POST['username'];
echo $username;
?>

它正在将“测试”行回显到正确的位置,因此它正在与 PHP 页面进行通信。另外,我知道我仍然有“文本”类型的 pwd 输入,并且我知道在将密码发送到服务器之前散列密码可能是个好主意。

谢谢大家的帮助!

4

4 回答 4

1

您没有在XMLHttpRequest. 就像是:

var params = "username=user&password=something";
xmlhttp.open("POST", "PHP/login.php", true);

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");

xmlhttp.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
        document.getElementById("rssnav2").innerHTML = xmlhttp.responseText;
    }
}
xmlhttp.send(params);
于 2013-08-17T23:43:47.410 回答
1

xmlhttp实际上并没有任何知识<form>,因此它不会自动发送任何数据。

相反,您必须自己收集<form>'s 数据,将其格式化为 URL 编码,并将.send()其与请求一起。

function loginSub(){
    var user = document.getElementById('username');
    var pass = document.getElementById('pwd');

    var postData = [
        encodeURIComponent(user.name) + '=' + encodeURIComponent(user.value),
        encodeURIComponent(pass.name) + '=' + encodeURIComponent(pass.value)
    ].join('&');

    // ...

    xmlhttp.send(postData);
}

有关更多详细信息,您可能需要通读 MDN 的Using XMLHttpRequest,尤其是关于仅使用AJAX的部分。

它包括一个在A little vanilla framework中收集<form>数据的通用解决方案,您可以使用它:

<form method="POST" action="PHP/login.php" onsubmit="AJAXSubmit(this); return false;">

边注:

<form>可能仍在提交,并且可能会中断 Ajax 请求。您可以通过取消其onsubmit事件来防止这种情况:

<form method="POST" id="userSub" class="userSub" onsubmit="return false">

此外,提交 a 的方法<form>比单击type="submit". 例如,大多数浏览器允许通过Enter在输入 atype="text"或时简单地点击来提交type="password"。而且,这样做通常不会模仿click.type="submit"

因此,您至少需要考虑将调用从onclickand 移到onsubmit

<form method="POST" id="userSub" class="userSub" onsubmit="loginSub(); return false;">
于 2013-08-17T23:47:05.087 回答
0

尝试改变

xmlhttp.send();

xmlhttp.send('username='+document.getElementById('username').value);

当然,您需要输入验证逻辑。

于 2013-08-17T23:46:22.013 回答
0
function loginSub()
{
var xmlhttp = false;
if(window.XMLHttpRequest)
{
    if(typeof XMLHttpRequest != 'undefined')
    {
        try
        {
            return xmlhttp= new XMLHttpRequest();
        }

        catch(e)
        {
            return xmlhttp= false;
        }
    }

    else if( window.ActiveXObject)
    {
        try
        {
            return xmlhttp= new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch(e)
        {
            try
            {
                return xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch(e)
            {
                return xmlhttp = false;
            }
        }
    }
}
var xmlhttp=false;
xmlhttp=createobject();
if(xmlhttp)
{   
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById('rssnav2').innerHTML = xmlhttp.responseText;
        }
    }
    parameter=username+'='+document.getElementById('usernname').value;
    xmlhttp.open('POST','PHP/login.php',true);
    xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    xmlhttp.send(parameter);
}

}

这将解决您的问题

于 2013-08-17T23:49:45.680 回答