Example of commodity comparison function implemented by PHP + ajax

Time:2020-2-25

In this paper, the example of PHP + Ajax to achieve the commodity comparison function. To share with you for your reference, as follows:

JS file of commodity comparison call (including commodity comparison box floating JS):

/*Floating window*/
(function(){
 var n=10;
 var obj=document.getElementById("goods-compare");
 if(!obj){
  return false;
 }
 var x=0;
 window.onscroll=function(){
  obj.style.top=(document.body.scrollTop||document.documentElement.scrollTop)+n+'px';
 };
 window.onresize=function(){
  obj.style.top=(document.body.scrollTop||document.documentElement.scrollTop)+n+'px';
 };
})();
//Add display contrast box
function addcompare(chk){
 $('#goods-compare').fadeIn().show();
 var count=$(".compare-box li").length;
 If (count > 2) // you can modify the comparison data here
 {
  Alert ('select 3 products at most for product comparison ');
  return;
 }
 $.ajax({
  type: 'post',
  url: 'ajax.php',
  data: {
   'action':'1',
   'GID': chk.gid, // commodity ID
   'gname': chk.gname, // commodity name
   'gType': chk. GType // product categories cannot be compared when they are different
  },
  cache: false,
  async: false,
  success: function(result) {
   if(result!='')
   {
    alert(result);
   }else{
    Var url ='http://www.lusen.com/product - '+ chk. GID +'. HTML '; // set the link address of the product
    $(". Compare box"). Append ("< Li class ='Division Clearfix 'id ='" + chk. GID + "') < div class ='span-3' > a href = '" + URL + "' target = '[blank' title = '" + chk. Gname + "'" '+ chk. Gname + "' < / a > < div > < span onclick =" removecompare ('"+ chk. GID +"'); \ "> delete < / span > < li >")
    $("comids"). Val ($(". Compare box Li"). Map (function() {// assign all commodity IDs of comparison to "comids"
     return $(this).attr('id');
    }).get().join(","));
   }
  }
 });
}
//Delete comparison products
function removecompare(id)
{
 $.ajax({
  type: 'post',
  url: 'ajax.php',
  data: {
   'action':'2',
   'gid':id
  },
  cache: false,
  success: function(result) {
   $("#"+id).remove();
   $("#comids").val($(".compare-box li").map(function(){
    return $(this).attr('id');
   }).get().join(","));
  }
 });
}
//Empty comparison products
function clearcompare()
{
 $.ajax({
  type: 'post',
  url: 'ajax.php',
  data: {
   'action':'3'
  },
  cache: false,
  success: function(result) {
   $(".compare-box").html('');
   $("#comids").val('');
  }
 });
}
//Show contrast box
function showcompare()
{
 $.ajax({
  type: 'post',
  url: 'ajax.php',
  data: {
   'action':'4'
  },
  success: function(result) {
   if(result){
    $(".compare-box").append(result);
    $("#comids").val($(".compare-box li").map(function(){
     return $(this).attr('id');
    }).get().join(","));
    $('#goods-compare').fadeIn().show();
   }
  }
 });
}
//Click to close the comparison box
$('.close-gc').click(function(){
 $('#goods-compare').fadeOut().hide();
});

Product comparison calls Ajax file:

<?php
function mb_unserialize($serial_str) {
 $serial_str =stripslashes($serial_str);
 return unserialize($serial_str);
}
if($_POST['action']=='1') {//add
 if(isset($_COOKIE['gtype'])) {
  if($_COOKIE['gtype']!=$_POST['gtype']) {
   Echo 'I'm sorry, you have selected products of different categories that cannot be added to the comparison. Please select similar products or clear the current comparison column and select again. ';
   return;
  }
 }else {
  setcookie('gtype',$_POST['gtype']);
 }
 if(isset($_COOKIE['gid'])) {
  $arr_str = $_COOKIE['gid'];
  $arr=mb_unserialize($arr_str);
  If (count ($ARR) > 2) {// product comparison quantity
   Echo "select at most 3 kinds of goods for comparison";
   return;
  }
  foreach($arr as $val) {
   if($val[0]==$_POST['gid']) {
    Echo "the product has been added to the comparison box";
    return;
   }
  }
  $info=array($_POST['gid'],$_POST['gname'],$_POST['gtype']);
  $arr[]=$info;
  $arr_str=serialize($arr);
  setcookie('gid',$arr_str);
 }else {
  $info=array($_POST['gid'],$_POST['gname'],$_POST['gtype']);
  $arr[]=$info;
  $arr_str=serialize($arr);
  setcookie('gid',$arr_str);
 }
}else if($_POST['action']=='2') {//delone
 $id=$_POST['gid'];
 $arr_str = $_COOKIE['gid'];
 $arr=mb_unserialize($arr_str);
 foreach($arr as $key=>$val) {
  if($val[0]==$id) {
   unset ($arr[$key]);
  }
 }
 $arr_str=serialize($arr);
 setcookie('gid',$arr_str);
}else if($_POST['action']=='3') {//delall
 setcookie('gid','');
 setcookie('gtype','');
}else if($_POST['action']=='4') {//showlist
 if(isset($_COOKIE['gid'])) {
  $data='';
  $arr_str = $_COOKIE['gid'];
  $arr=mb_unserialize($arr_str);
  foreach ($arr as $val){
   $url="http://www.lusen.com/product-".$val[0].".html";
   $data. = "< Li id = '{$Val [0]}' class ='Division Clearfix ': < div class ='span-3' > a href = '{$URL}' target = '[blank' title = '{$Val [1]}' {$Val [1]}) < / a > < div > < span onclick = \" removecompare ('{$Val [0]}'); \ "> delete < / span > < / Li >";
  }
  echo $data;
 }
}
?>

For more information about PHP, readers interested in PHP can refer to the following topics: PHP + Ajax skills and application summary, PHP network programming skills summary, PHP basic syntax tutorial, PHP object-oriented programming tutorial, PHP string usage summary, PHP + MySQL database operation tutorial and PHP common database operation skills summary Total “

I hope that this article will be helpful for PHP programming.

Recommended Today

Laravel service container must know

The article was forwarded from the professional laravel developer community. Original link: https://learnku.com/laravel/t To learn how to build an application with laravel is not only to learn how to use different classes and components in the framework, but also to remember allartisanCommand or all helper functions (we have Google). Learning to code with laravel is […]