-3

I have a php page with

<?php echo $_SERVER['DOCUMENT_ROOT']?>

Then my javascript is

var data = "Not Set";
$.get("test.php",function(returnData,requestStatus,requestObject){
    data = returnData;
    alert(data);
});

If I navigate directly to the php page on the site, it displays the data that I need. I just can't seem to get the data into my javascript.

Am I on the right track and if so where am I going wrong? Or is there an easier way to get the full filepath when working with a server? Currently if I run document.location.href in my javascript it returns .

http ://127.0.0.1/etc

xRuhRohx
  • 422
  • 1
  • 6
  • 31

3 Answers3

2

The code below will work on ".php" file. NOT ON ".html" file.

You can use the php variable with echo in javascript. For example

alert('<?=$phpvariable?>');

or

alert('<?php echo $phpvariable ?>');
Shail
  • 1,565
  • 11
  • 24
0

It seems you are overthinking this. There is hardly any need to use ajax, but of course you can and just append() the data from the ajax call to your $('body') and jquery will automatically execute things inside a <script> tag.

var serverRoot = '<?php echo $_SERVER['DOCUMENT_ROOT']?>';
Gung Foo
  • 13,392
  • 5
  • 31
  • 39
0

Try following

Instead of GET send $.post request and file don't return any thing just write

$.post("Requestedfile.php",
{ 
    data :data
},
function(data) {
    if(data == false)
    {
      //do something
    }
    else
    {
      //do somthing 
    }
}
);
kotanjan
  • 21
  • 3