【问题标题】:Sending data from JS (script's callback function) to PHP?将数据从 JS(脚本的回调函数)发送到 PHP?
【发布时间】:2012-02-17 02:52:55
【问题描述】:

我在 WordPress 中使用Thickbox,上传Thickbox 后将数据发送回编辑器(上传字段),整个jQuery 代码看起来就像here

jQuery(document).ready(function() {

   jQuery('#upload_image_button').click(function() { //user clicks upload button
     tb_show('', 'media-upload.php?type=image&TB_iframe=true'); //Thickbox pop-ups
     return false;
    });

    window.send_to_editor = function(html) { //here's our callback function
     imgurl = jQuery('img',html).attr('src');
     jQuery('#upload_image').val(imgurl); //that's how we send something to front-end
     //how to send "imgurl" to back-end within this function?
     tb_remove(); //closes Thickbox
    }

 });

我想知道将变量从 JS 发送到 PHP 的最佳方式是什么(我想将其发送到 WP 函数,以便使用 update_option('option_name','variableFromJS'); 将其注册为“选项”。

请记住,Thickbox 是在 iframe 中打开的,所以我只有这个回调函数。

【问题讨论】:

    标签: php javascript jquery post thickbox


    【解决方案1】:

    只需在函数中执行 $.post() 回到您的 PHP 页面。

    window.send_to_editor = function(html) { //here's our callback function
      imgurl = jQuery('img',html).attr('src');
      jQuery('#upload_image').val(imgurl); //that's how we send something to front-end
      //do an AJAX post back to the server (you'll probably want call backs, but this is simple
      $.post('media-upload.php',{ url: "coolURLToImage" });
      tb_remove(); //closes Thickbox
    }
    

    文档:http://api.jquery.com/jQuery.post/

    【讨论】:

      猜你喜欢
      • 2012-06-15
      • 2012-01-06
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      相关资源
      最近更新 更多