4

How to send and recieve info from php file with XMLHttpRequest?

I try using XMLHttpRequest and jquery ajax. In both cases work find, but I can't extract the data.

Js file

var parametros = {
            "url" : url
    };

  var xhr = new XMLHttpRequest();
  xhr.open("POST", "file.php", true);
  xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {

    alert(xhr.responseText)
  }
}
xhr.send(parametros);

file.php

  <?php 
$str = $POST['url'];
echo $str;

    ?>

The alert show me all the php file, with php tags (plain text). How to recieve only the $str var?

EDIT: manifest file:

{
  "name": "Get pages source",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Get pages source from a popup",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": ["http://localhost/extencio/*"]
}
Phyron
  • 613
  • 1
  • 9
  • 25

1 Answers1

2

That sounds more like an error on webserver, are you sure your webserver is executing the php file? You could test this by going to the location of the php file with your browser.

If it also shows all of the PHP in your webbrowser, take a look at this question: PHP code is not being executed, instead code shows on the page

Community
  • 1
  • 1
Mathijs
  • 373
  • 2
  • 10
  • Yes the webserver execute fine the php file. I am working on more php scripts and work fine. – Phyron Jan 14 '16 at 18:29