Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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<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) {


        });
share|improve this question

1 Answer 1

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'];
    // ...
}
share|improve this answer
    
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 Jun 24 at 7:51
    
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 Jun 24 at 8:28
    
@user3309362 Thanks, just updated my answer. –  Brandon Kelly Jun 24 at 8:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.