从JS(脚本的回调函数)发送数据到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 中打开的,所以我所做的一切得到的就是这个回调函数。
I'm using Thickbox with WordPress, after uploading Thickbox sends data back to editor (upload field), the whole jQuery code looks just like 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
}
});
I'm wondering what's the optimal way of sending variable from JS to PHP (I want to send it to WP function so it will got registered as an "option" using update_option('option_name','variableFromJS');
.
Keep in mind Thickbox is opening within an iframe, so all I have got is this callback function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在函数中执行 $.post() 返回 PHP 页面即可。
文档: http://api.jquery.com/jQuery.post/
Just do a $.post() back to your PHP page within the function.
Documentation: http://api.jquery.com/jQuery.post/