I'm trying to get data submitted from a form using craft()->request->getPost(), but I'm not sure what to do when the form name is an array element.

For example, I have a form with:

<label for="notes">Notes</label>
<textarea rows="6" id="notes" name="fields[notes]"></textarea>

and want to use:


but when I do this I don't get any data from the form, and I'm not sure what to put in getPost() to let it know its an array.

It will work fine if I change the name to "notes" and use getPost('notes'), but I'd like to be able to get it working with the array.

2 Answers 2


You can use dot notation:

  • nice answer selvin!
    – Ben Croker
    Commented Jun 27, 2014 at 15:48
  • Thanks! this is just what I needed. I'm doing something similar to this ContactForm example, and trying to do the same thing to get a file, should \CUploadedFile::getInstanceByName('fields.file') work the same way? This part is still not working for me and I'm not sure if it is supposed to work the same way or if there is some other error in my code.
    – user418
    Commented Jun 27, 2014 at 20:15
  • I got the file working using getInstanceByName('fields[emp_resume]'); which is what wouldn't work before. I am confused why it works here but not in the other case, and how I'm supposed to remember which way will work?
    – user418
    Commented Jun 27, 2014 at 20:35
  • In both instances, you're working with an associative array ($_POST, $_FILES) but the abstraction to find nested keys is implemented differently in for each purpose. For example, when you do craft()->request->getPost('fields.notes) you're saying "Craft, look at the $_POST array and find the passed in (dot notated) string" so Craft will check if there is a files key and then check if a notes key exist within the files keys. When you use \CUploadedFile::getInstanceByName('fields[file])` Yii will look for what you want in the $_FILES array not the $_POST array. Commented Jun 27, 2014 at 21:14
  • All of that to say that dot notation (fields.notes) and bracket notation (fields[file) are both syntactic sugar added on top of arrays. You could easily grab the first key from both and then look for the nested key within them in vanilla PHP or use the isset() technique. <!-- language: lang-php --> $fields = isset($_POST['fields']['notes']) ? $_POST['fields']['notes'] : false; $files = isset($_FILES['fields']['file']) ? $_FILES['fields']['file'] : false; if ($notes) { // You have notes... } if ($files) { // You have files... } Commented Jun 27, 2014 at 21:20


There might be cases where you want to check whether the post data is an actual array before using it, this isn't possible with the dot notation:

$data = craft()->request->getPost('fields');

if (is_array($data)) {
    //Do stuff...
else {
    //Do stuff...

For example the ContactForm plugin allows you to specify additional body fields that way:

One message field:    
<input id="msg" type="text" name="message" value="">

Multiple message fields:
<input id="phone" type="text" name="message[phone]" value="">
<input id="name" type="text" name="message[name]" value="">


Also like Selvin noted in the comments, you might want to check whether the field is actually set to prevent errors.

  • This is not safer than trying to access fields.notes directly and in fact, if you do this, you have a greater chance of running into undefined index because "notes" may not exist in the $data array. Commented Jun 27, 2014 at 13:56
  • You're right, I clarified my answer.
    – Victor
    Commented Jun 27, 2014 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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