[英]unable to get parameters from javascript to php
Unable to get parameters passed from javascript to loginme.php 无法获取从javascript传递到loginme.php的参数
This is simple form in index.php 这是index.php中的简单形式
<form method="POST">
<input type="text" id="userid" name="userid"></input>
<input type="password" id="pass" name="pass"></input>
<input type="button" value="Log in" onclick="letUserLogin()"/>
</form>
Javascript function : myscript.js Javascript函数: myscript.js
function letUserLogin() {
var userid = document.getElementById("userid").value;
var pass = document.getElementById("pass").value;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
alert(xmlhttp.responseText); //only shows 'and'
}
}
xmlhttp.open("POST","loginme.php?userid="+userid+"&pass="+pass,true);
xmlhttp.send();
}
Simple echo statement in loginme.php loginme.php loginme.php中的简单echo语句loginme.php
<?php
// username and password sent from form
$username=$_POST['userid'];
$password=$_POST['pass'];
echo"$username and $password";
?>
You are passing GET parameters: 您正在传递GET参数:
xmlhttp.open("POST","loginme.php?userid="+userid+"&pass="+pass,true);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
... thus you need to fetch them from $_GET
, not $_POST
: ...因此您需要从
$_GET
而不是$_POST
获取它们:
$username=$_GET['userid'];
$password=$_GET['pass'];
You possibly want to use the send() method instead to send your data. 您可能想使用send()方法来发送数据。 Right now, your payload is empty:
现在,您的有效负载为空:
xmlhttp.send();
You can resolve this with JQuery quite easily if you want: This method also allows you to put the URL within the action parameter of the form and uses POST which is more secured for transferring password information: 如果需要,您可以使用JQuery轻松解决此问题:此方法还允许您将URL放在表单的action参数内,并使用POST(传递密码信息更为安全):
JQUERY: JQUERY:
$(document).on('submit', "form", function(e){ //We add a listener
e.preventDefault();
$.post($(this).attr('action'), $(this).serialize())
.done( function( data ) {
//Do something with response
});
});
Note that you can of course change the listener to only listen to a specific form. 请注意,您当然可以将侦听器更改为仅侦听特定形式。 In this case all forms submits will be caught rather than a specific one in a page.
在这种情况下,将捕获所有提交的表单,而不是页面中的特定表单。
HTML: HTML:
<form action="/path/to/loginme.php">
<input type="text" name="userid">
<input type="password" name="pass">
</form>
PHP: PHP:
$username=$_POST['userid'];
$password=$_POST['pass'];
echo "$username and $password";
You are using POST
but explicitly setting the values into the query string, GET
style. 您正在使用
POST
但已将值显式设置为查询字符串GET
样式。 So basically you are sending a blank POST
. 因此,基本上,您正在发送空白
POST
。
You need to send the values like this: 您需要发送这样的值:
xmlhttp.open("POST","loginme.php", true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("userid=" + userid + "&pass=" + pass);
try this: 尝试这个:
var userid =encodeURIComponent(document.getElementById("userid").value)
var pass =encodeURIComponent(document.getElementById("pass").value)
var parameters="userid="+userid+"&pass="+pass
mypostrequest.open("POST", "loginme.php", true)
mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
mypostrequest.send(parameters)
use of encodeURIComponent()
to encode any special characters within the parameter values. 使用
encodeURIComponent()
对参数值内的任何特殊字符进行编码。 Call setRequestHeader()
and set its content type to "application/x-www-form-urlencoded"
. 调用
setRequestHeader()
并将其内容类型设置为"application/x-www-form-urlencoded"
。 This is needed for any POST request made via Ajax. 通过Ajax发出的任何POST请求都需要此文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.