<p>Any properties of the object you pass into <code>Craft.postActionRequest()</code>’s second argument will be available to Craft as POST parameters, so you can get to them with <a href="http://buildwithcraft.com/docs/apis/httprequestservice#getPost" rel="nofollow"><code>HttpRequestService::getPost()</code></a>:</p>

<pre><code>$sData = craft()-&gt;request-&gt;getPost('Data');

foreach ($sData as $temp)
{
    $wine = $temp['wine'];
    // ...
}
</code></pre>

CC BY-SA 3.0
2014-06-24T07:51:28.393Z
I think you're close... I think their data structure is actually an **array** of `temp` objects (containing "wine", etc). So I think they'll need to loop through `$data`, and pull the value of `wine` out of each object in the array.
Lindsey D
CC BY-SA 3.0
2014-06-24T08:28:02.123Z
I have added the full code above maybe you can get a better idea why I dont think i can do $data = craft()->request->getPost('Data'); as i have many of these 'Data' varaibles inside another array
user3309362
CC BY-SA 3.0
2014-06-24T08:57:54.293Z
@user3309362 Thanks, just updated my answer.
Brandon Kelly
CC BY-SA 3.0
2014-06-24T07:22:52.440Z
2014-06-24T08:57:34.143Z
2014-06-24T08:57:34.143Z
Brandon Kelly
<p>I have a JavaScript file which sends an array of data to a controller: </p>

<pre><code>Craft.postActionRequest('wineNotes/wineSpreadSheet/saveWines', data, ...)
</code></pre>

<p>The structure of the data variable is as follows:</p>

<pre><code>var data = {
    Data: sData
}
</code></pre>

<p>... where <code>sData</code> is an array of temp variables <code>sData(temp1,temp2,...)</code> and the structure of each temp object is like this:</p>

<pre><code>temp = {
    "wine":        ht.getDataAtCell(i,0),
    "country":     ht.getDataAtCell(i,1),
    "region":      ht.getDataAtCell(i,2),
    "note":        ht.getDataAtCell(i,3),
    "tastingdate": ht.getDataAtCell(i,4),
    "rating":      ht.getDataAtCell(i,5),
    "maturity":    ht.getDataAtCell(i,6),
    "conclusion":  ht.getDataAtCell(i,7)
};
</code></pre>

<p>How can I access the parameter <code>temp.wine</code> for each <code>temp</code> in the <code>sData</code> array within the following method of my controller?</p>

<pre><code>public function actionSaveWines()
{
    // ???
}
</code></pre>

<p>I know the data structure is kinda wierd its actually an array of arrays. Read the below code and you might understand.</p>

<pre><code>for(i=0;i&lt;NumRows;i++)
        {
            temp = {
                "wine":ht.getDataAtCell(i,0),
                "country":ht.getDataAtCell(i,1),
                "region":ht.getDataAtCell(i,2),
                "tastingdate":ht.getDataAtCell(i,3),
                "rating":ht.getDataAtCell(i,4),
                "maturity":ht.getDataAtCell(i,5),
                "conclusion":ht.getDataAtCell(i,6),
            };
            sData.push(temp);
        }

        var data = {
            Data: sData,
        }
        Craft.postActionRequest('wineNotes/wineSpreadSheet/saveWines', data, function(response) {


        });
</code></pre>

<plugin-development><controller><ajax>
How do I access AJAX data within a controller method?
# How do I access AJAX data within a controller method?

(Asked by: username_0 on 2014-06-24)

I have a JavaScript file which sends an array of data to a controller: 
Craft.postActionRequest('wineNotes/wineSpreadSheet/saveWines', data, ...)

The structure of the data variable is as follows:
var data = {
    Data: sData
}

... where sData is an array of temp variables sData(temp1,temp2,...) and the structure of each temp object is like this:
temp = {
    "wine":        ht.getDataAtCell(i,0),
    "country":     ht.getDataAtCell(i,1),
    "region":      ht.getDataAtCell(i,2),
    "note":        ht.getDataAtCell(i,3),
    "tastingdate": ht.getDataAtCell(i,4),
    "rating":      ht.getDataAtCell(i,5),
    "maturity":    ht.getDataAtCell(i,6),
    "conclusion":  ht.getDataAtCell(i,7)
};

How can I access the parameter temp.wine for each temp in the sData array within the following method of my controller?
public function actionSaveWines()
{
    // ???
}

I know the data structure is kinda wierd its actually an array of arrays. Read the below code and you might understand.
for(i=0;i

**Tags:** plugin-development, controller, ajax

---

## 1 answers

### Answer by username_1 on 2014-06-24. Score: 4

Any properties of the object you pass into Craft.postActionRequest()’s second argument will be available to Craft as POST parameters, so you can get to them with HttpRequestService::getPost():
$sData = craft()->request->getPost('Data');

foreach ($sData as $temp)
{
    $wine = $temp['wine'];
    // ...
}

> **Comments:**
> **username_2**: I think you're close... I think their data structure is actually an **array** of `temp` objects (containing "wine", etc). So I think they'll need to loop through `$data`, and pull the value of `wine` out of each object in the array.  
> **username_0**: I have added the full code above maybe you can get a better idea why I dont think i can do $data = craft()->request->getPost('Data'); as i have many of these 'Data' varaibles inside another array  
> **username_1**: @user3309362 Thanks, just updated my answer.  

---

