Search code examples
phpjqueryajaxpost

How do I convert a json array into an array php can read?


Answer

Thanks to Kenneth's answer and suggestions, I did figure out what I needed to do. There's an added layer because this is an ExpressionEngine site, but basically I needed to tell the function that the variable was coming in from the post, like this:

$newItems = $this->EE->input->post('newItems');
$newItems = json_decode($newItems, true);

UPDATED

After following Kenneth's suggestion below, I was able to get the javascript to recognize that I was passing in r.newItems. But my php function doesn't recognize that it's receiving an array. This is the error I'm getting:

PHP Warning:  Missing argument 1 for Order::addReorderToCart()

My php function looks like this:

public function addReorderToCart($newItems) {
        error_log("newitems");
        error_log(print_r($newItems,1)); // this is not printing anything
        $_SESSION['order']['items'] = array_merge($_SESSION['order']['items'], $newItems);
        $this->EE->app->addToCart();
        $rtnArray['success'] = true;
    }

What do I need to do to translate the array that's being sent through the jquery so that the php function recognizes it as an array?

I have the following javascript/jquery code that runs as a result of another ajax call:

function ohReorderAjaxReturn(data) {
    console.log("ajax return");
    console.dir(data);

    var r = eval(data);
    console.log("r");
    console.dir(r);

    if(data.unavailableItems instanceof Array && data.unavailableItems.length > 0) {
        // there are unavailable items; ask if they want to go ahead

        Modal({
            title: r.errTitle,
            text: r.errMsg, // need to update this to contain correct text including store address and unavailableItems
            yellow_button: {
                btn_text: "It's OK, continue",
                action: function(r){
                    console.log("r inside function");
                    console.log(r);
                    // need ajax call to addReorderToCart
                    $.post('/site/ajax/order/addReorderToCart',  {'newItems': r.newItems},
                    function(data) {
                        var ret = eval(data);

                        if( ret.success == 1 ) {
                            document.location = '/site/order_summary';
                        }
                        else {
                            alert("error");
                        }
                    });
                }
            },
            black_button: {
                btn_text: "Cancel"
            }
        });
    }
    else {
        console.log("not an array");
    // there are no unavailable items; add to cart and continue on
    }
}

A console.log right inside the if(data.unavailableItems instanceof Array && data.unavailableItems.length > 0) line lets me know that it gets that far. It is popping up the modal, but the action part (which calls another php function via ajax) doesn't seem to be getting the newItems value passed in. Here's a screenshot of Firebug; you can see that the console.dir(r) a few lines inside the function signature returns information for newItems. Why does r become undefined when I try to pass it into the post? And how can I do it, if what I'm doing now is wrong?

enter image description here


Solution

  • That's because you define r as a parameter of your function and when the function is called it's not passing a value and thus the parameter is hiding the access to your outer variable. You should remove the parameter and then your outer variable will become visible inside the function again:

    ** snip **
    yellow_button: {
                    btn_text: "It's OK, continue",
                    action: function(){   // removed r as a parameter
                        console.log("r inside function");
                        console.log(r);
    ** snip **