4

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?

5
  • It should on click load php file into div friends Commented Apr 4, 2015 at 7:13
  • use _("right").innerHTML = '<?php echo include_once "friendlist.php";?>'; Commented Apr 4, 2015 at 7:16
  • Are you sure the file (on which you are running this) is .php ? Commented Apr 4, 2015 at 7:16
  • 1
    What you want is not doable. You cannot use javascript to dynamic load php files. Commented Apr 4, 2015 at 7:16
  • 1
    your problem is that the server isnt parsing .js files as .php check this stackoverflow.com/questions/9368841/… wither you name is as .js.php or edit server config and make it parse .js for php as well Commented Apr 4, 2015 at 7:35

6 Answers 6

4

you have to do like this

$(document).ready(function(){
  $("#your_id").click(function(){
    $("#your_div").load('friendlist.php');
  });
});
Sign up to request clarification or add additional context in comments.

Comments

3

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>

5 Comments

How can I do it onclick?
@User check my updated answer .. second option also work for me you can call friends() on click event
RETURN: Uncaught ReferenceError: friends is not defined
@User have you create friends() function ?
@User may be you facing problem due to content inside your php file. check in your console how function look . first try with simple string echo . it will work
1

it's Impossible because php works on server not in the browser. use:

$("div").load("php_file_name")

5 Comments

why it is Impossible? have you test any example ?
no not working. a`m repeat,only ajax request can do this because browser can't run php code. it's must do server
it is Impossible and php running?.show my and i never write php code again
Do you think I post my answer without any testing??
yes man it's working. 2-3 years old ago browser can't do this.
0
<script>   
 $(document).ready(function(){
      $("#id").click(function(){
        $("#div").load('friendlist.php');
      });
    });
</script>

try like this...

Comments

0

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.

1 Comment

Thanx you, I am sure my friendlist.php echoes html data
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.