简体   繁体   English

使用Ajax制作添加监视列表按钮功能(PHP)

[英]Using Ajax to make add watchlist button feature (PHP)

My HTML Code: 我的HTML代码:

<form class="addtowatchlistform" action="logo/insertwatchlist.php" method="POST">

    <input type="hidden" name="tmdb_id" value="'.$result[$x]["tmdb_id"].'"/>
    <button id="addtowatchlistbutton" type="submit" name="tmdb_id" value="'.$result[$x]["tmdb_id"].'" data-tooltip="'.$addremove.' TO YOUR WATCHLIST" class="material-icons" style="color:'.$watchlisticoncolor.'">add_box</button>

</form>

   // Same form as above
<form class="addtowatchlistform" action="logo/insertwatchlist.php" method="POST">

    <input type="hidden" name="tmdb_id" value="'.$result[$x]["tmdb_id"].'"/>
    <button id="addtowatchlistbutton" type="submit" name="tmdb_id" value="'.$result[$x]["tmdb_id"].'" data-tooltip="'.$addremove.' TO YOUR WATCHLIST" class="material-icons" style="color:'.$watchlisticoncolor.'">add_box</button>

</form>

Jquery code: jQuery代码:

<script>
$(".addtowatchlistform").submit(function(e) {
  var data = $(this).serialize();
  var url = $(this).attr("action");
  $.post(url, data, function() {

     try {
        data = JSON.parse(data);
        $("button#addtowatchlistbutton").data('tooltip', data.addremove + " TO YOUR WATCHLIST");
        $("button#addtowatchlistbutton").css('color',data.watchlisticoncolor);
    } catch (e) {
        console.log("json encoding failed");
        return false;
    }

  });
  return false;
});
</script>

insertwatchlist.php code insertwatchlist.php代码

<?php
$response = new \stdClass();
$response->addremove = "item1";//you can get the data anyway you want(e.g database)
$response->watchlisticoncolor = "red";
die(json_encode($response));
?>

Expected Result: 预期结果:
1.)When someone clicks on add_box button, it submits the form without reloading the page (This one works fine) 1.)当某人单击add_box按钮时,它无需重新加载页面即可提交表单(此方法很好用)

2.) insertwatchlist.php sends this code: {"addremove":"item1","watchlisticoncolor":"red"} and, the Jquery code place them in place of: $addremove and $watchlisticoncolor variable, in the real time without reloading the page. 2.) insertwatchlist.php发送以下代码: {"addremove":"item1","watchlisticoncolor":"red"} ,并且jQuery代码将它们实时放置在$addremove$watchlisticoncolor变量的位置,而无需重新加载页面。

Original Result: 原始结果:

First point of expected result works fine. 预期结果的第一点效果很好。

Second point of expected result do nothing (no error) 预期结果的第二点不执行任何操作(无错误)

data is not defined at $.post() callback function. $.post()回调函数未定义data Though you have already defined data before $.post() call. 尽管您已经在$.post()调用之前定义了data Use a different identifier name 使用其他标识符名称

$.post(url, data, function(response) { // define `response` here
  $("button#addtowatchlistbutton")
  .data('tooltip', response.addremove + " TO YOUR WATCHLIST")
  .css('color',response.watchlisticoncolor);
})

JSON.parse() is not necessary. 不需要JSON.parse() jQuery.post() converts JSON from server to a JavaScript object jQuery.post()JSON从服务器转换为JavaScript对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM