【问题标题】:PHP sending HTML to JSPHP 向 JS 发送 HTML
【发布时间】:2010-02-11 22:13:15
【问题描述】:

我在页面上有一个 div,其内容根据下拉列表中选择的值而变化。因为这个 div 中有大量格式化的内容,所以我希望有一个 php 脚本提供 html,它存储在系统上的一个文件中。

我无法弄清楚如何让 php 返回该文件的内容,该文件本质上包含许多 div 标记和内容,作为 ajax 脚本的文本。然后会更改页面上 div 中的内容。

后端php使用CakePHP,前端框架使用YUI3。

如果需要,解决方案不需要包含任何 Cake。

谢谢!

【问题讨论】:

    标签: php ajax cakephp header yui


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      “我无法弄清楚如何让 php 返回该文件的内容”

      echo file_get_contents( $filepath );
      

      对于网站中的 ajax 部分(尽管您似乎没有要求):

      var xmlhttp;
      if( window.XMLHttpRequest )
          xmlhttp = new XMLHttpRequest();
      else if( window.ActiveXObject )
          xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );     // Use ActiveX in IE
      else {
          alert( 'Your browser doesn\'t support XMLHttpRequest.' );
          return;
      }
      xmlhttp.eid = eid; // <-- id of the div to be filled with the content from the file
      xmlhttp.onreadystatechange = function() {
          if( xmlhttp.readyState == 4 ) {
              document.getElementById( xmlhttp.eid ).innerHTML = xmlhttp.responseText;
          }
      }
      xmlhttp.open( "GET", "phpfile.php", true );
      xmlhttp.send( null );
      

      编辑:请考虑此解决方案可能与您的框架不同。

      【讨论】:

      • 谢谢 - 你的方法也有效!我正在使用 YUI3,所以 AJAX 的东西已经处理好了。 +1
      【解决方案3】:

      您还可以为每种类型的 html 表单创建视图并通过 ajax 助手返回它。它会给你更多的灵活性;你甚至可以在这个 html 表单中添加一些 php 逻辑等等......

      在文件中存储格式化的 html 表单过于原始,因为很容易想象需要用数据库中的内容填充一些标签的情况。用“视图”来做会给你这个机会。

      最好的问候。

      【讨论】:

      • 是的,我们已经考虑过该选项,但在这种情况下,最好的情况是存储静态 html 文件。不过还是谢谢。
      猜你喜欢
      • 1970-01-01
      • 2020-03-12
      • 2020-06-15
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多