简体   繁体   English

使用Ajax和php获取愿望清单中项目的多个数据加载

[英]Using Ajax & php to get multiple data onload for items in a wishlist

I am trying to implement, an ajax function that will take the id's of products on load and checks against the db to see if that user has it in their wishlist, then use that information to change the color of wishlist icon.This happens on load of the page. 我正在尝试实现一个ajax函数,该函数将在加载时获取产品的ID,并对照db进行检查以查看该用户是否在其愿望清单中,然后使用该信息来更改愿望清单图标的颜色。页面。

Currently the function runs but it only looks at the first item, and functions as intended to but not on the remaining products. 当前,该功能正在运行,但它仅查看第一项,并且按预期功能运行,但不针对其余产品运行。

Any help on this would be really appreciated, many thanks 在这方面的任何帮助将不胜感激,非常感谢

The javascript JavaScript

<script type="text/javascript">
window.onload = function(){

       var link_data = $(".add-wishlist").data('data');   
       $.ajax({
          type: "POST",
          url: '../source/wishlist_control.php',
          data: ({fav_check: link_data}),
          success: function(data) {

               if(data.replace(/\s+/, "")  == 'YES')
               {
                  $('a[data-data="' + link_data + '"] > i.whishstate').css({"color":"#EB686C"})

               }
               else if (data.replace(/\s+/, "")  == 'NO'){
                   $('a[data-data="' + link_data + '"] > i.whishstate').css({"color":"gray"})

               }
               else {

               }
          } 
       });

    }; 

</script>

The icon with the id that is being printed the page 带有ID的图标正在打印页面

<a class="add-wishlist" href='javascript:;' data-data='<?php echo protect($usri['id']); ?>'>
<i class='fa fa-heart whishstate'></i>
</a>

The php file code php文件代码

if(isset($_POST['fav_check'])) {  
    $addmemberid = $_SESSION['user_id'];
    $addproductid = $_POST['fav_check'];
    $result = mysql_query("SELECT count(product_id) cnt FROM users_wishlist WHERE user_id = '$addmemberid' AND product_id = '$addproductid'") or die(mysql_error());
    $countid = mysql_fetch_assoc($result);
    if($countid['cnt'] == 1){
        echo 'YES';
    } else {
         echo 'NO';
    }
}

The jquery selector will only select the data first element matched. jQuery选择器将仅选择匹配的数据第一个元素。 to do that to all elements you have to loop over them using jQuery each function: 要对所有元素执行此操作,则必须使用jQuery 每个函数遍历它们:

$(".add-wishlist").each(function() {
   var link_data = $(this).data('data');   
   $.ajax({
      type: "POST",
      url: '../source/wishlist_control.php',
      data: ({fav_check: link_data}),
      success: function(data) {

           if(data.replace(/\s+/, "")  == 'YES')
           {
              $('a[data-data="' + link_data + '"] > i.whishstate').css({"color":"#EB686C"})

           }
           else if (data.replace(/\s+/, "")  == 'NO'){
               $('a[data-data="' + link_data + '"] > i.whishstate').css({"color":"gray"})

           }
           else {

           }
      } 
});

Instead of putting AJAX call in a loop, try creating an array of wishlist IDs and POST that to server in a single AJAX call. 与其将AJAX调用置于循环中,不如尝试创建一个愿望清单ID数组,并在单个AJAX调用中将其发布到服务器。 Then in server you can fetch the respective data and return that as array to the AJAX success function. 然后在服务器中,您可以获取相应的数据,并将其作为数组返回给AJAX成功函数。 This will save multiple AJAX request and increase the performance. 这将保存多个AJAX请求并提高性能。
So the code will look something like this: 因此,代码将如下所示:

var link_data = [];
$(".add-wishlist").each(function() {
  link_data.push($(this).data('data'));
});
$.ajax({
  type: "POST",
  url: '../source/wishlist_control.php',
  data: ({fav_check: link_data}),
......
......

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

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