Example of implementing commodity comparison function by php+ajax
- 2021-12-05 05:50:51
- OfStack
In this paper, an example is given to describe the realization of commodity comparison function by php+ajax. Share it for your reference, as follows:
The JS file called by the commodity comparison (including the floating JS of the commodity comparison box):
/* 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 a 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(' Product comparison has the most choices 3 Plant oh ');
return;
}
$.ajax({
type: 'post',
url: 'ajax.php',
data: {
'action':'1',
'gid':chk.gid,// Commodity ID
'gname':chk.gname,// Commodity name
'gtype':chk.gtype// Commodity categories. You can't compare when the categories 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(){// All the goods that will be compared ID , assign a value 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();
});
Commodity 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 ' Sorry, you selected a different category of products can not be added to the comparison, please select similar products or clear the current comparison bar before selecting. ';
return;
}
}else {
setcookie('gtype',$_POST['gtype']);
}
if(isset($_COOKIE['gid'])) {
$arr_str = $_COOKIE['gid'];
$arr=mb_unserialize($arr_str);
if(count($arr)>2) {// Comparative quantity of goods
echo " Commodity comparison has the most choices 3 Species ";
return;
}
foreach($arr as $val) {
if($val[0]==$_POST['gid']) {
echo " This 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 readers interested in PHP related contents, please check the topics on this site: "Summary of PHP+ajax Skills and Applications", "Summary of PHP Network Programming Skills", "Introduction to PHP Basic Grammar", "Introduction to php Object-Oriented Programming", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"
I hope this article is helpful to everyone's PHP programming.