2012-02-17 64 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/