将数据从JS(脚本的回调函数)发送到PHP


Sending data from JS (script's callback function) to PHP?

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

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页面执行$.post()即可。

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/