【问题标题】:JavaScript innerHTML include phpJavaScript innerHTML 包括 php
【发布时间】:2015-06-09 05:26:19
【问题描述】:

为什么我的代码工作? P>

function _(x){
    return document.getElementById(x);
}

function friends(){
    _("right").innerHTML = '<?php include_once "friendlist.php";?>';
}

结果:

<!--?php include_once "friendlist.php";?-->

我该如何解决?

【问题讨论】:

  • 应该点击加载php文件到DIV朋友 SPAN>
  • 使用_( “右”)的innerHTML = ''; 跨度>
  • 你确定该文件(在其上运行此)为 .PHP B>? SPAN>
  • 你想要的是不可行的。你不能使用JavaScript来动态负载PHP文件。 SPAN>
  • 你的问题是,作为服务器的心不是解析.js文件.PHP检查这个stackoverflow.com/questions/9368841/…枯萎你的名字是.js.php或编辑服务器配置,使其解析的.js为PHP以及跨度>

标签: javascript php html innerhtml


【解决方案1】:

你必须这样做

$(document).ready(function(){
  $("#your_id").click(function(){
    $("#your_div").load('friendlist.php');
  });
});

【讨论】:

    【解决方案2】:

    使用下面的代码就可以了

     <div id="right">
       <script>
          document.write('<?php echo include_once "include.php";?>');
       </script>
    </div>
    

    或者我测试下面的代码也可以工作

     <body>
         <div id ="right" >
    
         </div>
      </body>
    
      <script>
        function friends(){
         var x=document.getElementById("right");
         x.innerHTML = '<?php include_once "include.php";?>';
         }      
        friends();    
     </script>
    

    【讨论】:

    • 点击后怎么办?
    • @User 检查我的更新答案.. 第二个选项也适用于我,你可以在点击事件时调用 friends()
    • RETURN: Uncaught ReferenceError: friends is not defined
    • @User 你创建了friends()函数吗?
    • @User 可能是您由于 php 文件中的内容而面临的问题。检查您的控制台功能的外观。首先尝试使用简单的字符串 echo 。它会工作
    【解决方案3】:
    <script>   
     $(document).ready(function(){
          $("#id").click(function(){
            $("#div").load('friendlist.php');
          });
        });
    </script>
    

    试试这样...

    【讨论】:

      【解决方案4】:

      这是不可能的,因为 php 在服务器上工作而不是在浏览器中。 使用:

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

      【讨论】:

      • 为什么不可能?你测试过任何例子吗?
      • 没有不工作。重复一遍,只有 ajax 请求可以这样做,因为浏览器无法运行 php 代码。它必须做服务器
      • 这是不可能的并且 php 正在运行?.show my and i never write php code again
      • 你认为我在没有任何测试的情况下发布我的答案吗??
      • 是的,它正在工作。 2-3年前的浏览器无法做到这一点。
      【解决方案5】:

      如果您的代码可以工作,那将是严重的安全漏洞。好在你不能通过浏览器注入和执行 php 代码:) 你可以将 php 标签放在 html 文件中,你可以显示它,但它甚至不会通过服务器,更不用说执行它了。

      另一方面,您可以在server 上创建friendlist.php 并确保:

      比你在browser/client 中做你的html/js

      _("right").innerHTML = ajax('http://yout_site.com/friendlist.php');
      

      顺便说一句,如果您使用 JavaScript 从服务器请求某些内容,然后在浏览器中显示它,则称为 ajax ;)

      为了好玩,您也可以使用 http://www.upiur_site.com/friendlist.php'> 来避免使用 ajax。但这有很多缺点。 对于ajax函数使用库,自己写...

      编辑:

      如果服务器设置为解析 .js 文件,我错过了第三个选项。在这种情况下会起作用。

      【讨论】:

      • 谢谢你,我确定我的friendlist.php 与html 数据相呼应
      【解决方案6】:

      我知道这是被询问和回答的,而且它与静态内容有关,但值得指出的是,这些答案(非 ajax)需要刷新整个页面才能更新 php 内容。这是因为 php 包含由服务器编译,然后发送到浏览器,其中 javascript 现在只有编译结果。

      阅读本文的用户可能希望创建动态内容,在这种情况下,更好的解决方案可能是让 javascript 获取页面。这可以通过以下方式实现。

      <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> 
      

      参考:
      https://www.experts-exchange.com/questions/29091951/Include-php-file-with-JS.html https://www.w3schools.com/xml/xml_http.asp

      如果需要在计时器上刷新内容,也可以使用 javascript setTimeout() 函数。

      请随时改进我的答案或将其附加到其他类似问题。

      【讨论】:

        猜你喜欢
        • 2019-02-23
        • 2016-04-24
        • 1970-01-01
        • 2014-06-02
        • 1970-01-01
        • 2015-11-16
        • 2019-03-17
        • 1970-01-01
        • 2015-10-17
        相关资源
        最近更新 更多