获取 jQuery $.post 将数据发送到 PHP 文件

发布于 2025-01-01 16:45:52 字数 1127 浏览 0 评论 0原文

我正在尝试使用 jQuery 将一些数据发布到 PHP 文件。如果我通过表单发送数据,一切都会正常,但我希望它通过 jQuery 在后台发送。点击功能有效($.post 除外),因为我已经使用 alert() 对其进行了测试,并且当我注释掉 $.post 时> 一切正常。如果我不注释掉 $.post 行,最后两行将不起作用。

这是我存储在 admin.js 中的 javascript:

$(document).ready(function(){

    $(".admin-refresh").click(function () {
        var movieID = $(this).prev().text();
        $.post("actions.php", {refreshMovie: yes, movieID: movieID});
        $(this).removeClass('btn-warning');
        $(this).addClass('btn-success');
    });

});

这是 actions.php 中的一些代码。如果我通过表单发布数据,该文件就可以工作。

//Refresh Movie Details
if ($_POST['refreshMovie']) {

  $movieID = $_POST['movieID'];

以下是 active-movies.php 中的代码,其中包含激活 JavaScript 的按钮。

<button class="btn admin-refresh"><i class="icon-refresh"></i> Refresh</button>

这些文件存储为 ROOT/admin/active-movies.phpROOT/admin/actions.phpROOT/includes/js/admin。 js

感谢您提供的任何帮助!

I'm trying to post some data to a PHP file using jQuery. If I send the data via a form everything works just fine, but I want it to send in the background via jQuery. The click function works (except $.post), because I have tested it with an alert() and when I comment out the $.post line everything else works. If I don't comment out the $.post line the last two lines don't work.

Here is my javascript stored in admin.js:

$(document).ready(function(){

    $(".admin-refresh").click(function () {
        var movieID = $(this).prev().text();
        $.post("actions.php", {refreshMovie: yes, movieID: movieID});
        $(this).removeClass('btn-warning');
        $(this).addClass('btn-success');
    });

});

Here is some of the code from actions.php. This file is working if I post the data via form.

//Refresh Movie Details
if ($_POST['refreshMovie']) {

  $movieID = $_POST['movieID'];

Here is the code from active-movies.php, which contains the button that activates the javascript.

<button class="btn admin-refresh"><i class="icon-refresh"></i> Refresh</button>

The files are stored as such ROOT/admin/active-movies.php, ROOT/admin/actions.php, and ROOT/includes/js/admin.js.

Thank you for any help you can offer!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

挽你眉间 2025-01-08 16:45:52

至少你的问题之一就在这里。

{refreshMovie: yes, movieID: movieID}

应该是

{refreshMovie: "yes", movieID: movieID}

At least one of your problems is here.

{refreshMovie: yes, movieID: movieID}

should be

{refreshMovie: "yes", movieID: movieID}
孤独岁月 2025-01-08 16:45:52

尝试向 $.post() (回调)添加第三个参数,例如:

$.post("actions.php", {refreshMovie: yes, movieID: movieID}, function(response){
    // actions.php should return some data to check if the 
    // action was successful
    // that data will be available as a variable ("response")
   if ( response == 'success' ) {
       // do something
   } else {
       // do something else
   }
});

Try adding a third argument to the $.post() (a callback) like:

$.post("actions.php", {refreshMovie: yes, movieID: movieID}, function(response){
    // actions.php should return some data to check if the 
    // action was successful
    // that data will be available as a variable ("response")
   if ( response == 'success' ) {
       // do something
   } else {
       // do something else
   }
});
自由如风 2025-01-08 16:45:52

只需将参数名称用引号引起来,因为 JS 会错误地认为 movieID:movieID 类似于 Kung Fu Panda:Kung Fu Panda

{'refreshMovie': 'yes', 'movieID': movieID}

Just enclose the parameters names into quotes, because the JS will make the mistake to think that movieID:movieID is like Kung Fu Panda:Kung Fu Panda.

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