代码之家  ›  专栏  ›  技术社区  ›  Steven

javascript/jQuery与PHP的结合

  •  0
  • Steven  · 技术社区  · 16 年前

    我有以下代码/脚本:

      <?php
        $uploadifyPath = get_bloginfo('url') . '/wp-content/plugins/uploadify/';
        $galleryPath = "'".getGalleryURL('1620')."'"; // <--- 1620 is inputed by me. 
      ?>
    
      <input id="galleryID" type="hidden" value="1620" name="galleryID"/>
      <input id="fileInput" name="fileInput" type="file" />
    
      <script type="text/javascript">// <![CDATA[
        $(document).ready(function() {
          $('#fileInput').uploadify({
              'uploader'  : '<?php echo $uploadifyPath ?>uploadify.swf',
              'script'    : '<?php echo $uploadifyPath ?>uploadify.php',
              'cancelImg' : '<?php echo $uploadifyPath ?>cancel.png',
              'auto'      : true,
              'folder'    : <?php echo $galleryPath ?>
          });
        });
      // ]]></script>
    

    getGalleryURL() ?

    3 回复  |  直到 16 年前
        1
  •  1
  •   fresskoma    16 年前

    你不能。你知道PHP代码是在Web服务器上执行的。然后,HTML/CSS/JS代码被传输到浏览器,在浏览器中执行javascript。

    如果需要Javascript/PHP通信,则必须使用jQuerys AJAX功能。

        2
  •  1
  •   Blair McMillan    16 年前

    通过jQuery执行AJAX调用,让PHP知道galleryID,然后使用它的回调来加载uploadify。

        3
  •  0
  •   dmzza    11 年前

    我完全接受了这个挑战,不得不为我正在进行的一个项目想办法。

    我想到的更简单:在脚本之前在HTML中回显变量,这样jQuery就可以从data属性中提取变量。

    我还没有测试下面的代码,但我想你可以用类似的方法来解决这个问题。祝你好运!

    <div class="marker" data-path="<?php echo get_bloginfo('url') . '/wp-content/plugins/uploadify/'; ?>" data-url="<?php echo getGalleryURL('1620'); ?>" style="display:none;"></div>
    
    <input id="galleryID" type="hidden" value="1620" name="galleryID"/>
    <input id="fileInput" name="fileInput" type="file" />
    
    
    <script type="text/javascript">// <![CDATA[
        $(document).ready(function() {
    
    
          var path = $('.marker').data('path');
          var url = $('.marker').data('url');
    
          $('#selector').uploadify({
              'uploader'  : url + '/uploadify.swf',
              'script'    : url + '/uploadify.php',
              'cancelImg' : url + '/cancel.png',
              'auto'      : true,
              'folder'    : path
          });
        });
    
        // ]]>
    </script>
    
    推荐文章