Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I convert a json array into an array php can read?
    text
    copied!<p><strong>Answer</strong></p> <p>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:</p> <pre><code>$newItems = $this-&gt;EE-&gt;input-&gt;post('newItems'); $newItems = json_decode($newItems, true); </code></pre> <p><strong>UPDATED</strong></p> <p>After following Kenneth's suggestion below, I was able to get the javascript to recognize that I was passing in <code>r.newItems</code>. But my php function doesn't recognize that it's receiving an array. This is the error I'm getting:</p> <pre><code>PHP Warning: Missing argument 1 for Order::addReorderToCart() </code></pre> <p>My php function looks like this:</p> <pre><code>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-&gt;EE-&gt;app-&gt;addToCart(); $rtnArray['success'] = true; } </code></pre> <p>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?</p> <p>I have the following javascript/jquery code that runs as a result of another ajax call:</p> <pre><code>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 &amp;&amp; data.unavailableItems.length &gt; 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 } } </code></pre> <p>A <code>console.log</code> right inside the <code>if(data.unavailableItems instanceof Array &amp;&amp; data.unavailableItems.length &gt; 0)</code> line lets me know that it gets that far. It is popping up the modal, but the <code>action</code> part (which calls another php function via ajax) doesn't seem to be getting the <code>newItems</code> value passed in. Here's a screenshot of Firebug; you can see that the <code>console.dir(r)</code> a few lines inside the function signature returns information for <code>newItems</code>. Why does <code>r</code> 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?</p> <p><img src="https://i.stack.imgur.com/KuhEs.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload