JQuery dont show some data true json

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?

Hi,

I didn’t really understand your question.
The best thing to do is to make a short, self-contained code example which demonstrates your problem and that people can run for themselves. Then rephrase your question stating what you have tried and what didn’t work.

Here are some more tips on how to phrase your question and maximize your chances of getting help:

Here is some more information site is on web address http://www.mojtestdomen.net84.net
When you chose category link you have for each category product (its books) when i click Add to basket in Ajax part You Basket should be price, tax and number of items when click Remove From Basket that’s should remove the item with that id. Add to basket link is php 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;
}

Then in this javascript file i have file to check this link with id, to get rel who have id for product rel is example rel=2_0, then i splitted rel to get just id, id is 2 and 0 is Name for link if is 0 then it’s Add to basket if is 1 then it’s Remove from basket.

$(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;
               var new_id = item[0] + '_' + data.job;
               console.log("Id is " + item[0]);
               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;
    });
}

});

From this code i get only job i can’t get id even if i get id in console these code for jquery work. It think the problem is now in basketprod.php file code in that file is:

require_once (‘…/inc/autoload.php’);
require_once ‘…/classes/Catalogue.php’;
require_once ‘…/classes/Session.php’;
require_once ‘…/classes/Basket.php’;
require_once ‘…/classes/Business.php’;
require_once ‘…/classes/Dbase.php’;
require_once ‘…/classes/Helper.php’;
require_once ‘…/classes/Paging.php’;
require_once ‘…/classes/Url.php’;
require_once ‘…/classes/application.php’;

//if sent data from basket.js - if send rel splited rel=2_1 now is 2 and 1 job is 2 id is 1

if (isset($_POST[‘job’]) && isset($_POST[‘id’]))
{

$out = array();
$job = $_POST['job'];
$id = $_POST['id'];
$objCatalogue = new Catalogue();
$product = $objCatalogue->getProduct($id);
if (!empty($product)) 
{
    switch($job) 
    {
        case 0:
        Session::removeItem($id);
        $out['job'] = 1;
        break;
        case 1:
        Session::setItem($id);
        $out['job'] = 0;
        break;
    }
echo json_encode($out);
}

}

and code from Session class is

class Session
{
public static function setItem($id, $qty = 1)
{
//create session basket with index of id of specific product and quantity
$_SESSION[‘basket’][$id][‘qty’] = $qty;
}

public static function removeItem($id, $qty = null)
{
    if($qty != null && $qty < $_SESSION['basket'][$id]['qty'])
    {
        $_SESSION['basket'][$id]['qty'] = ($_SESSION['basket'][$id]['qty'] - $qty);
    }
    else
    {
        //set to null basket if removet item
        $_SESSION['basket'][$id] = null;
        unset($_SESSION['basket'][$id]);
    }
}

public static function getSession($name = null)
{
    if(!empty($name))
    {
        return isset($_SESSION[$name]) ? $_SESSION[$name] : null;
    }
}
//end class

}

When I click on the link i get as I say only job of second array from rel
and get these message {“job”:0}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.