Reputation: 314
Why doesn't my code work?
function _(x){
return document.getElementById(x);
}
function friends(){
_("right").innerHTML = '<?php include_once "friendlist.php";?>';
}
Result:
<!--?php include_once "friendlist.php";?-->
How can I fix it?
Upvotes: 4
Views: 42218
Reputation: 318
I know this is asked and answered and also that it is in relation to static content, however its worth pointing out that these answers (non ajax) require the whole page to be refreshed in order to update the php content. This is because the php include is compiled by the server and then sent to the browser where the javascript now has only the results of the compilation.
Users reading this may wish to create dynamic content, in which case a better solution may be to have the javascript fetch the page. This can be achieved as follows.
<script type="text/javascript">
function updatePrices() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "http://example.com/current_prices.php", true);
xhttp.send();
}
</script>
<div id="demo">
<h2>Change This</h2>
</div>
<button type="button" onclick="updatePrices();">Click Here!</button>
ref:
https://www.experts-exchange.com/questions/29091951/Include-php-file-with-JS.html
https://www.w3schools.com/xml/xml_http.asp
A javascript setTimeout() function may also be used if the content needs to be refreshed on a timer.
Please feel free to improve my answer or to append it to other similar questions.
Upvotes: 0
Reputation: 3914
If your code could work, it would be serious breach of security. Good thing is that you CAN NOT inject and execute php code by your browser:) You can put php tags in html file, you can display it, but it will not even pass by the server, let alone execute it.
On the other hand you CAN create friendlist.php on the server
and make sure:
.
Than you do your html/js in browser/client
_("right").innerHTML = ajax('http://yout_site.com/friendlist.php');
Btw, if you use JavaScript to request something from server and then display it in browser it's called ajax ;)
Just for fun, you could also use http://www.upiur_site.com/friendlist.php'> to avoid ajax. But that has many disadvantages. For ajax function use library, write your own...
EDIT:
I missed third option if server is set to parse .js files. In that case will work.
Upvotes: 0
Reputation: 79
it's Impossible because php works on server not in the browser. use:
$("div").load("php_file_name")
Upvotes: 1
Reputation: 6031
use below code it will work
<div id="right">
<script>
document.write('<?php echo include_once "include.php";?>');
</script>
</div>
or i test below code also it will work
<body>
<div id ="right" >
</div>
</body>
<script>
function friends(){
var x=document.getElementById("right");
x.innerHTML = '<?php include_once "include.php";?>';
}
friends();
</script>
Upvotes: 3
Reputation: 2447
<script>
$(document).ready(function(){
$("#id").click(function(){
$("#div").load('friendlist.php');
});
});
</script>
try like this...
Upvotes: 0
Reputation: 1545
you have to do like this
$(document).ready(function(){
$("#your_id").click(function(){
$("#your_div").load('friendlist.php');
});
});
Upvotes: 4