简体   繁体   中英

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

I am kind of new to Javascript, PHP and 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:

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

My test.js file:

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:

<?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. 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".

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. 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 .

Then you say $q = $_REQUEST['kruispunt'];

The key kruispunt is not q .

Meanwhile, the data you do send in the POST request — xhttp.send("kruispunt"); … is a plain text string and not URL encoded form data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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