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

时间:2012-02-17 02:52:55

标签: php javascript jquery post thickbox

我正在使用带有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中打开的,所以我所拥有的就是这个回调函数。

1 个答案:

答案 0 :(得分: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/