Hy, I need some help with my code for jQuery.
In PHP i have code
public static function activeButton($sess_id) {
if(isset($_SESSION['basket'][$sess_id])) {
$id = 0;
$label = "Remove from basket";
} else {
$id = 1;
$label = "Add to basket";
}
$out = "<a href=\"#\" class=\"add_to_basket";
$out .= $id == 0 ? " red" : null;
$out .= "\" rel=\"";
$out .= $sess_id."_".$id;
$out .= "\">{$label}</a>";
return $out;
}
which work and this code give link: <a class="add_to_basket" rel="9_1" href="#">Add to basket</a>
in rel =9_1 nine is id for id product from database an 1 is number that change name of link, when is 1 then is Add to basket when is zero then its Remove from basket.
That’s all work my problem is with Ajax
$(document).ready(function (){
function refreshSmallBasket(){
$.ajax({
url: "mod/basket_small_refresh.php",
dataType: 'json',
success: function (data) {
//foreach data arrayk is key v is value
$.each(data, function (k, v){
//in basket_left id for each classes for reffering index k and span filling with value v
$("#basket_left ." + k + " span").text(v);
});
},
error: function(xhr, status, errorThrown) {
//alert("An error has occurred");
console.log("Proble Load");
console.log("Error: " + errorThrown);
console.log("Status: " + status);
console.dir(xhr);
}
});
}
//check that class exist i our case add_to_basket class
if($(".add_to_basket").length > 0){
//on click in this class add_to_basket
$(".add_to_basket").click(function(){
//triiger is add_to_basket class
var trigger = $(this);
//parma is add_to_basket basket class on click and get attribut rel our rel is exapmple re=2_1
var param = trigger.attr("rel");
console.log("rel is " + param);
//this rel split with _ then we have 2 and 1
var item = param.split("_");
console.log("Split rel is " + item);
//add ajax
$.ajax({
type: 'POST',
url: 'mod/basketprod.php',
dataType: 'json',
data: ({ id: item[0], job: item[1] }),
success: function (data){
//create new rel example rel=2_0
var new_id = data.id + '_' + data.job;
//get job but id is undefine
console.log("Id is " + data.id);
console.log("rel is " +data.job);
console.log("new rel is " + new_id);
//if id different then job
if(data.job != item[1]){
//if job 0
if(data.job == 0){
//add to add_to_basket class new rel with new id
trigger.attr("rel", new_id);
//add new text
trigger.text("Remove From Basket");
//add class red
trigger.addClass("red");
}
else{
trigger.attr("rel", new_id);
trigger.text("Add to basket");
trigger.removeClass("red");
}
refreshSmallBasket();
}
},
error: function(xhr, status, errorThrown) {
//alert("An error has occurred");
console.log("Proble Load");
console.log("Error: " + errorThrown);
console.log("Status: " + status);
console.dir(xhr);
}
});
//not loading link
return false;
});
}
});
In second ajax call i have problem for id, in code var param = trigger.attr(“rel”); i get id with job 9_1 then I split rel var item = param.split(""); i get velues 9,1 problem is in var new_id = data.id + '’ + data.job; i get undefined id bud job is working its change. How to fix this problem?