Combining javascript / jQuery and PHP

▼魔方 西西 提交于 2019-12-08 12:23:08

问题


I'm using Uploadify to upload an image. Now I need to get the correct upload path.

I have the following code / script:

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

How can I, with jQuery, get the value of galleryID and input it into my function getGalleryURL() ?

Or... is there a better way to do this??


回答1:


You can't. You're PHP code is executed on the webserver. Then, the HTML/CSS/JS code is transmitted to the browser, where javascript is executed.

If you need Javascript/PHP communication, you will have to use jQuerys AJAX functionality.




回答2:


Do an AJAX call via jQuery to let PHP know the galleryID and then use it's callback to load uploadify.




回答3:


Totally get this challenge, had to figure it out for a project I am working on just now.

What I came to was more simple: echo out the variable in the HTML before the script so then jQuery can pull the variable from the data attribute.

I haven't tested the code below yet but I imagine you could solve this with something similar. Good luck!

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


来源:https://stackoverflow.com/questions/1774036/combining-javascript-jquery-and-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!