0

is it possible to load content AND variables from a *.php-File with xmlhttprequest?

I have a index.php:

<script>
function loadsite() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("divrequest").innerHTML = this.responseText;
        }
    };
    xhttp.open("GET", "siterequest.php", true);
    xhttp.send();
}
$(document).ready(loadsite());

</script>

<div id="divrequest"></div>

My siterequest.php:

<?php
    echo "some dynamic content";
    echo json_encode(array($var1,$var2,$var3));
    echo "more dynamic content";
?>

Am I able to get the variables? Or did I misunderstand the function of XMLHttpRequest?

EDIT: If I use

var myvariable = JSON.parse(JSON.stringify(xhttp.responseText));
console.log(myvariable);

I will get the code of the whole page.

undefined
  • 25
  • 9
  • Assuming you're talking about variables stored within the target's DOM, it's absolutely possible. After parsing the page contents, simply extract the parts of it that you would like to assign to a variable. To show you exactly how to do that, you'll need to provide the code you're trying to scrape, and let us know the variable you'd like to extract. If you're trying to extract the `GET` variables, that's also [**very possible**](https://stackoverflow.com/a/901144/2341603). PHP variables cannot be extracted themselves, but if you **output** them, you can extract them from the DOM. – Obsidian Age Jul 04 '17 at 20:44
  • split() out the JSON from the whole page, then parse it. or use $.load(), which eval()s ` – dandavis Jul 04 '17 at 20:53
  • they should be displayed with `echo json_encode(array($var1,$var2,$var3));` – undefined Jul 04 '17 at 20:55
  • Use fetch, it's 2k17 ;) – Angels Jul 04 '17 at 20:59
  • `JSON.parse(JSON.stringify(xhttp.responseText));` should be just `JSON.parse(xhttp.responseText);`, if that response is all JSON. – dandavis Jul 04 '17 at 20:59
  • @Angels: there's a lot of "old" phones out there that still can't play `fetch()`... – dandavis Jul 04 '17 at 21:00
  • @dandavis: To use $.load() I have to rewrite the whole code I already have - maybe my last option in this case ;) Could you show me, how to split() out the JSON? I've never used it before and only found `str.split` on google. – undefined Jul 04 '17 at 21:16
  • 1
    you need a delimeter of some sort, newlines, reserved char, etc. ex: `echo '#'.json_encode(array($var1,$var2,$var3)).'#';` .. `JSON.parse(xhttp.responseText.split("#")[1]);`, where `#` is not literally present in the data itself. – dandavis Jul 04 '17 at 21:24
  • @dandavis: Great idea! It works - with only one variable, otherwise with `JSON.parse(xhttp.responseText.split("#")[1]);` I'll get the whole array like `Array [ "1499241999", "lol", "rofl" ]` - but it`s enough. I only need one variable at the moment :) Thank you very much! – undefined Jul 05 '17 at 08:07

1 Answers1

0

XMLHttpRequest sends an HTTP-request to the server to access the desired page. PHP is executed before the response data is sent back to you, in which case it's translated to HTML. Therefore, you can't get the variables using this approach with JavaScript. That happens before you even receive the data.

Max
  • 897
  • 1
  • 10
  • 27
  • sorry, but javascript is a dynamic language that continues running with a malleable lexical scope – dandavis Jul 04 '17 at 20:54
  • @dandavis Is this answer wrong? If so, wouldn't that imply that I can get information about the PHP-file which generates the HTML I'm receiving as a client, only using regular HTTP-requests as a means of communication? Have I misunderstood the question or something? I thought the OP was asking if he could retrieve the PHP variables from the page he's requesting using XMLHttpRequest – Max Jul 04 '17 at 20:58
  • @Max No, I want to "extract" the variables from the PHP-File. I know, that I can't retrieve the variables. ;) e.g. echo the variables in the *.php-Page and get what they contain – undefined Jul 04 '17 at 21:01
  • if they are shipped to js from php via ajax, and the js handles it correctly, you can indeed define new variables post-load, and those can be fed by php values – dandavis Jul 04 '17 at 21:01
  • @undefined Okay, now I see what you mean. I misunderstood the question. Yes, that should be possible, and you seem to be discussing it already so I'll just leave it – Max Jul 04 '17 at 21:04