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

发布于 2025-01-07 00:15:52 字数 976 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骷髅 2025-01-14 00:15:52

只需在函数中执行 $.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/

Just do a $.post() back to your PHP page within the function.

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
}

Documentation: http://api.jquery.com/jQuery.post/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文