Geolassi
Geolassi

Reputation: 31

Receive POST request in PHP from OpenLayers JavaScript

I have problems in receiving POST request in PHP. I'm using JavaScript to send data to a PHP page with POST request. The JavaScript is from OpenLayers.js, and the part that sends the request looks like this:

var postrequest = OpenLayers.Request.POST({
        url: "http://localhost/index.php",
        data: "success",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        }
    });

In PHP, I'm using this code to see, what I'm getting:

<?php
    print_r($_POST);
?>

This is what happens:

  1. index.php receives POST request.
  2. FireBug also informs that POST Parameters contain Success, the one that was sent.
  3. print_r($_POST); in index.php just gives this: array() and doesn't change after the POST request from JavaScript.

So the data is sent and received, but my PHP code doesn't somehow understand it, or I'm not using the right PHP function.

Any suggestions, where to look, and what to try?

Upvotes: 2

Views: 2728

Answers (2)

Lahza
Lahza

Reputation: 1

I guess you need include XMLHttpRequest.js library, you can download it from this link

https://github.com/ilinsky/xmlhttprequest

Upvotes: 0

James
James

Reputation: 22247

I think the "data" property needs to be an object containing key/value pairs.

eg:

var postrequest = OpenLayers.Request.POST({
        url: "http://localhost/index.php",
        data: {
          userName: "myUsername",
          password: "myPassword"
        },
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        }
    });

If this works when you print_r($_POST) you should see array("userName" => "myUsername", "password" => "myPassword")

Upvotes: 5

Related Questions