I';我正在尝试使用带有post方法的AJAX向php文件发送用户名和密码。如何检索字段
如何访问这些变量? 我试图在php文件中检索用户名和密码,但如果我使用I';我正在尝试使用带有post方法的AJAX向php文件发送用户名和密码。如何检索字段,php,html,ajax,Php,Html,Ajax,如何访问这些变量? 我试图在php文件中检索用户名和密码,但如果我使用$\u POST['username'} <script> var a = new XMLHttpRequest(); a.onreadystatechange = function(){ if (a.readyState==4 && a.status==200) { ajaxFinished = true;
$\u 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>
var a=新的XMLHttpRequest();
a、 onreadystatechange=函数(){
如果(a.readyState==4&&a.status==200){
ajaxFinished=true;
警报(a.responseText);
}
否则{
}
}
a、 打开(“post”,“php/psswd.php”,true);
a、 发送(“'Username=“+document.getElementByNames(“Username”)+”&Password=“+getElementByNames(“Password”);//发布用户名和密码
如何在php文件中检索这些字段?语法是
getElementsByName
,而不是您目前使用的getElementByNames
单词Elements是复数的,而不是名称
<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>
与其使用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>
$('#loginForm')。提交(函数(e){
var username=$(“#登录用户名”).val();//表单文本输入的id
password=$(“#登录密码”).val();//表单文本输入的id
e、 预防默认值();
$.ajax({
类型:“POST”,
url:url,
数据:{格式:'login',用户名:'+username+'',密码:'+password+'}
}).success(函数(msg){
//msg是将验证登录的PHP函数返回的文本
$('.login_success_text').html(msg);
});
});
用户名
密码
我自己找到了答案,问题是,
a、 setRequestHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
需要添加。和文档。getElementsByName('xyz')返回节点列表,但不是特定的节点,我们需要遍历该节点列表。它应该是
$\u POST['Username']
为什么要使用}
?请尝试在a.send
删除单引号。对不起,输入错误,我使用的是$\u POST['Username']只有Tanks Dave,但没有任何更改。旁注:在依赖表单中的POST变量时,变量是区分大小写的,如果是这样的话。Username
和Username
是两种完全不同的动物。不,我已经将其更改为document.getElementsByName,仍然不起作用还有一个问题,如何等待响应?这是哪种反应?@user3413403我仍然收到未识别的索引用户名作为错误消息。我的两只眼睛都在同一个插座中,已经过了凌晨2点,我无法继续思考。如果没有其他人给出答案,我将在明天上午提供帮助。同时,如果你能看到这一点,可能会有所帮助。@user3413403
<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>