2012-02-17 67 views
0

我使用ThickBox的与WordPress,上传后ThickBox的数据发送回编辑器(上载字段),整个jQuery代码看起来就像是here从JS(脚本的回调函数)发送数据到PHP?

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

只要做一个$ .post()回到你的PHP页面的f结。

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/