1

I've a situation here I've got a basket stored in session there is a delete item button on user end. I want to return a null array if there is not item in the array

Here is my PHP code

session_start();

header('Content-Type: application/json');
require_once('includes/requireonce.php');

if ( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) :

    if(isset($_POST['removeItem'])){

        $itemToRemove = htmlspecialchars($_POST['removeItem']);

        if(count($_SESSION['order_array']) <= 1){
            unset($_SESSION['order_array']);
            print json_encode([null]);
        }else{
            unset($_SESSION['order_array'][$itemToRemove]);
            sort($_SESSION['order_array']);
            print json_encode($_SESSION['order_array']);
        }
    }
endif;

jQuery function that calls that code and prints the data on screen:

$('#fixedBasket').on('click', '.removeItemCLS', function() {
    var itemIndex = $(this).attr('id'),
        cart_total = 0;
    // console.log('remove Item new: ' + itemIndex);

    $.post("delete_item.php",
    {
        removeItem : itemIndex
    },
    function(data){
        if(data){
            $("#cart_result").empty();
            $(data).each(function(index,item) {
                output = setCartContent(index, item);
                cart_total += +item.price;
                $("#cart_result").append(output);
            });

            $("#cart_total").empty();
            $("#cart_total").html(cart_total.toFixed(2));
        }else{
            $("#cart_result").empty();
            $("#cart_total").empty();
        }
    });

    return false;
});

setCartContent below

function setCartContent(index, data){
    return '<div class="row">' + 
        '<div class="col-md-2"><a href="#" id="' + index + '" class="removeItemCLS"> <span class="glyphicon glyphicon-minus-sign" aria-hidden="true"></span> </a></div>' +
        '<div class="col-md-7">' + data.product_name + '</div>' + 
        '<div class="col-md-3"><div class="pull-right">' +  data.price + '</div></div>' + 
    '</div><br />';
}

I want to return a null and catch a null on jQuery side so if it is null just empty the div's

Any ideas?

Regards

1
  • print json_encode(array()) ? Commented Feb 19, 2015 at 16:29

1 Answer 1

3

try this

print json_encode([]);

that will return an empty or null array

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.