1

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

3
  • 2
    Why eval? Are you using it just as a JSON parser? Commented May 22, 2013 at 17:46
  • I'm adding to code that someone else wrote a while back. The eval has always worked until we added this new bit of functionality, so I didn't change it. Commented May 22, 2013 at 17:48
  • Can you post the Order class ? Or otherwise the code that is invoked by the action url. Commented May 22, 2013 at 19:03

1 Answer 1

1

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 **
Sign up to request clarification or add additional context in comments.

7 Comments

I'm pretty sure I already tried that, but I've gone through so many permutations that maybe I missed this one. I'll try it again.
Hm. OK, that got me past that part, but my php error log is showing that addReorderToCart isn't receiving a parameter. So for some reason, r.newItems is not actually making through from the javascript to the php.
Is your PHP expecting JSON?
Ah, that could be the problem. No, it isn't. So if I use json_decode($newItems) in my php, that should fix it? I'll give it a shot.
No, it probably won't fix it. Your PHP is looking for form values, not JSON. I'm not familiar with PHP so you'd have to look somewhere else for that question
|

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.