Pogledajte određenu poruku
Staro 03. 11. 2014.   #1
stanke
član
Certified
 
Datum učlanjenja: 06.09.2012
Poruke: 64
Hvala: 0
342 "Hvala" u 10 poruka
stanke is on a distinguished roadstanke is on a distinguished roadstanke is on a distinguished roadstanke is on a distinguished road
Default Ajax problem sa json-om

Pozdrav imam jedan problem ajax mi ne vraca neke podatke preko jsona
u PHP imam kod

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;
}

i on daje link <a class="add_to_basket" rel="3_1" href="#">Add to basket</a>
3 je id iz baze a 1 je vrednost koja sluzi za menjanje imena linka ako je 1 onda je Add to basket ako je 0 onda je Remove from basket.
$sess_id je id iz baze za odgovarajuci proizvod.
To sve radi perfektno. Problem je u Ajax preko json-a ne mogu da dobijem id proizvoda npr. 3_1 ne vraca mi 3 uopste kod je sledeci:

$(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;
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;
});
}
});

U drugom po redu ajax-u, json ne vraca id. iznad ovog ajaxa ima kod za umizanje rel
var param = trigger.attr("rel"); dobijem tacnu vrednos 3_1, u ajaxu razdvojim rel var new_id = data.id + '_' + data.job; i dobijem 3,1 a kad hocu da spojim rel ponovo var new_id = data.id + '_' + data.job; onda dobijem rel=undefined_0, id ne mogu nikako da vratim kako bi uzeo proizvod iz baze.
Molim za pomoc

Poslednja izmena od stanke : 03. 11. 2014. u 12:27.
stanke je offline   Odgovorite uz citat