繁体   English   中英

为什么我的PHP看不到javascript后请求?

[英]Why doesn't my PHP see the javascript post- request?

我是Java,PHP和AJAX的新手。 我花了很长时间寻找这个问题的答案,但找不到答案。 首先是我的代码:

我的index.html文件:

<input type="text" id="value" onkeyup="loadDoc(this.value)">
        <p id="demo"></p>
        <p id="demo2"></p>

我的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();
  }
   }
  }

我的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);
   ?>

我想要实现的是将我的输入值存储在“ demo”中,并同时将该参数(kruispunt)赋予我的.php文件。 然后,我希望我的.php文件找出$ x和$ y是什么,并将其发送回myFunction()并将php文件中的$ x和$ y变量放入“ demo2”中。

例如,如果我将5用作输入值,则“ demo”的确会返回5,但是在“ demo2”中什么都没有显示之后,因此我认为POST或.php文件出了点​​问题。 我以某种方式在浏览器中没有出现错误,但是也没有任何显示。

我真的希望我能阐明我要实现的目标,并在此先感谢您解决或帮助解决我的问题!

您说的是q=" + kruispunt 。因此,您的查询字符串(如果要发送查询字符串中的数据,为什么要发出POST请求?)具有键q

然后,您说$q = $_REQUEST['kruispunt'];

关键kruispunt不是q

同时,您在POST请求中发送的数据— xhttp.send("kruispunt"); …是纯文本字符串,不是URL编码的表单数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM