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
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']?>';
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 ?>');
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
}
}
);