-2

Is it possible a html/javascript page can retrieve the value of a variable from other php page?

For example test.php and main.html are 2 pages. test.php just echo the value of one variable V. do you know if main.html could retrieve the value V from test.php.

one idea could be something like a tag with attribute src=test.php?

redline
  • 3
  • 1

4 Answers4

0

The answer is Yes. You could make an ajax post or get request to that php page and get values. You could use jquery to make the process easier. i.e.:

main.html

<script src="pathtojs/jquery.js"></script>
<script>
var V;
$.get("test.php",function(data){
    V = data.V;
},"json")
</script>

test.php

<?php
$data = array("V" => "your variable value");
echo json_encode($data);
Yoel Nunez
  • 2,108
  • 1
  • 13
  • 19
-1

Are you navigating form one pahe to the other? If so, a cookie or localstorage could do the trick. A query string parameter could also work.

-1

Not exactly, but there is a workaround in the form of AJAX. I personally prefer using jQuery. You can use the jQuery $.get(); method to retrieve the variable.

$(document).ready(function() {
   var phpVar;
   $.get("test.php", function(data){
      phpVar = data;
   });
});

with the test.php file, you need to echo $var; otherwise the data parameter of the $.get(); function will be blank. It contains everything that is generated on test.php so if you only generate your variable, it will only contain your variable

Colum
  • 996
  • 2
  • 8
  • 23
-1

ok ajax is one method (for me not preferable,but also main.html is only html,not php echoing), but what about iframes?

for example if main.html had <iframe src=http://serverDomain/test.php></iframe> and test.php echo some javascript which using DOM can innerHTML the value of variable in the main page.

redline
  • 3
  • 1