[英]Why doesn't my PHP see the javascript post- request?
I am kind of new to Javascript, PHP and AJAX. 我是Java,PHP和AJAX的新手。 I searched for a long time for an answer to this problem but couldn't quite find the answer.
我花了很长时间寻找这个问题的答案,但找不到答案。 First off here is my my code:
首先是我的代码:
My index.html file: 我的index.html文件:
<input type="text" id="value" onkeyup="loadDoc(this.value)">
<p id="demo"></p>
<p id="demo2"></p>
My test.js file: 我的test.js文件:
function loadDoc(kruispunt) {
var xhttp;
xhttp=new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = kruispunt;
}
};
xhttp.open("POST","link.php?q=" + kruispunt, true);
xhttp.send("kruispunt");
}
function myFunction(){
var xhttp;
xhttp=new XMLHttpRequest();
xhttp.onload = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo2").innerHTML = this.responseText;
xhttp.open("GET","link.php", true);
xhttp.send();
}
}
}
My link.php file: 我的link.php文件:
<?php
$kruispunt5= file_get_contents('http://fiwarelab.ckan.nl/api/action/datastore_search? resource_id=0077d99e-127c-4c28-acde-c0f337e13065');
$kruispunt5 = json_decode($kruispunt5, true);
$lat5 = json_encode($kruispunt5['result']['records'][4]['latitude']);
$long5 = json_encode($kruispunt5['result']['records'][4]['longitude']);
$kruispunt11 = file_get_contents('http://fiwarelab.ckan.nl/api/action/datastore_search? resource_id=6b39a68b-54d1-4254-a2ce-af59a8856f3f');
$kruispunt11 = json_decode($kruispunt11, true);
$lat11 = json_encode($kruispunt11['result']['records'][4]['latitude']);
$long11 = json_encode($kruispunt11['result']['records'][4]['longitude']);
$q = $_REQUEST['kruispunt'];
$x = 0;
$y = 0;
if ($q !== "") {
if ($q === "5"){
$x = $lat5;
$y = $long5;
} else if ($q === "11"){
$x = $lat11;
$y = $long11;
}
}
echo json_encode($x);
echo json_encode ($y);
?>
What I want to achieve is that my inputvalue gets stored in "demo" and at the same time to give that parameter (kruispunt) to my .php file. 我想要实现的是将我的输入值存储在“ demo”中,并同时将该参数(kruispunt)赋予我的.php文件。 Then I want my .php file figure out what $x and $y is and send that back to myFunction() and put the $x and $y variables from the php file into my "demo2".
然后,我希望我的.php文件找出$ x和$ y是什么,并将其发送回myFunction()并将php文件中的$ x和$ y变量放入“ demo2”中。
If I put 5 as my inputvalue for example, "demo" does return 5 but after that nothings shows in "demo2" so I think there is something wrong with either my POST or my .php file. 例如,如果我将5用作输入值,则“ demo”的确会返回5,但是在“ demo2”中什么都没有显示之后,因此我认为POST或.php文件出了点问题。 I somehow don't get an error in my browser, but nothing shows either.
我以某种方式在浏览器中没有出现错误,但是也没有任何显示。
I really hope i made clear what I wanted to achieve and thanks in advance for solving or helping with my problem! 我真的希望我能阐明我要实现的目标,并在此先感谢您解决或帮助解决我的问题!
You said q=" + kruispunt
. So your query string (why are you making a POST request if you're going to send the data in a query string?) has the key q
. 您说的是
q=" + kruispunt
。因此,您的查询字符串(如果要发送查询字符串中的数据,为什么要发出POST请求?)具有键q
。
Then you say $q = $_REQUEST['kruispunt'];
然后,您说
$q = $_REQUEST['kruispunt'];
The key kruispunt
is not q
. 关键
kruispunt
不是q
。
Meanwhile, the data you do send in the POST request — xhttp.send("kruispunt");
同时,您在POST请求中发送的数据—
xhttp.send("kruispunt");
… is a plain text string and not URL encoded form data. …是纯文本字符串,不是URL编码的表单数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.