Answers.Body: <p>Note:</p>
  
  <p>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:</p>
  
  <pre class="lang-php prettyprint-override"><code>$data = craft()-&gt;request-&gt;getPost('fields');
  
  if (is_array($data)) {
      //Do stuff...
  }
  else {
      //Do stuff...
  }
  </code></pre>
  
  <p>For example the <a href="https://github.com/pixelandtonic/ContactForm" rel="nofollow">ContactForm</a> plugin allows you to specify <a href="https://github.com/pixelandtonic/ContactForm#adding-additional-fields" rel="nofollow">additional body fields</a> that way:</p>
  
  <pre class="lang-html prettyprint-override"><code>One message field:    
  &lt;input id="msg" type="text" name="message" value=""&gt;
  
  Multiple message fields:
  &lt;input id="phone" type="text" name="message[phone]" value=""&gt;
  &lt;input id="name" type="text" name="message[name]" value=""&gt;
  </code></pre>
  
  <p>.</p>
  
  <p>Also like Selvin noted in the comments, you might want to check whether the field is actually set to prevent errors.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T13:56:18.177Z
Answers.Text: 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.
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T16:26:50.730Z
Answers.Text: You're right, I clarified my answer.
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T13:48:42.683Z
Answers.LastActivityDate: 2014-06-27T16:26:51.677Z
Answers.LastEditDate: 2014-06-27T16:26:51.677Z
Answers.UserDisplayName: Victor
Answers.Body: <p>You can use dot notation:</p>
  
  <pre class="lang-php prettyprint-override"><code>craft()-&gt;request-&gt;getPost('fields.notes');
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T15:48:20.750Z
Answers.Text: nice answer selvin!
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T20:15:23.503Z
Answers.Text: Thanks! this is just what I needed.  I'm doing something similar to this [ContactForm](https://github.com/pixelandtonic/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.
Answers.UserDisplayName: user418
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T20:35:58.100Z
Answers.Text: 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?
Answers.UserDisplayName: user418
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T21:14:38.903Z
Answers.Text: 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.
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T21:20:47.323Z
Answers.Text: 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... }
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-27T13:41:42.350Z
Answers.LastActivityDate: 2014-06-27T14:03:33.363Z
Answers.LastEditDate: 2014-06-27T14:03:33.363Z
Answers.UserDisplayName: Selvin Ortiz
Body: <p>I'm trying to get data submitted from a form using <code>craft()-&gt;request-&gt;getPost()</code>, but I'm not sure what to do when the form name is an array element.</p>
  
  <p>For example, I have a form with:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;label for="notes"&gt;Notes&lt;/label&gt;
  &lt;textarea rows="6" id="notes" name="fields[notes]"&gt;&lt;/textarea&gt;
  </code></pre>
  
  <p>and want to use:</p>
  
  <pre class="lang-php prettyprint-override"><code>craft()-&gt;request-&gt;getPost('fields[notes]');
  </code></pre>
  
  <p>but when I do this I don't get any data from the form, and I'm not sure what to put in <code>getPost()</code> to let it know its an array.</p>
  
  <p>It will work fine if I change the name to "notes" and use <code>getPost('notes')</code>, but I'd like to be able to get it working with the array.</p>
  
Tags: <plugins>
Title: getPost() on array element in plugin
ThreadText: # getPost() on array element in plugin
  
  (Asked by: username_0 on 2014-06-27)
  
  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:
  
  ```html
  <label for="notes">Notes</label>
  <textarea rows="6" id="notes" name="fields[notes]"></textarea>
  
  ```
  
  and want to use:
  
  ```php
  craft()->request->getPost('fields[notes]');
  
  ```
  
  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.
  
  **Tags:** plugins
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2014-06-27. Score: 10 | Accepted answer
  
  You can use dot notation:
  
  ```php
  craft()->request->getPost('fields.notes');
  
  ```
  
  > **Comments:**
  > **username_3**: nice answer username_2!  
  > **username_0**: Thanks! this is just what I needed.  I'm doing something similar to this [ContactForm](https://github.com/pixelandtonic/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.  
  > **username_0**: 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?  
  > **username_2**: 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.  
  > **username_2**: 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... }  
  
  ---
  
  ### Answer by username_1 on 2014-06-27. Score: 1
  
  Note:
  
  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:
  
  ```php
  $data = craft()->request->getPost('fields');
  
  if (is_array($data)) {
      //Do stuff...
  }
  else {
      //Do stuff...
  }
  
  ```
  
  For example the [ContactForm](https://github.com/pixelandtonic/ContactForm) plugin allows you to specify [additional body fields](https://github.com/pixelandtonic/ContactForm#adding-additional-fields) that way:
  
  ```html
  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 username_2 noted in the comments, you might want to check whether the field is actually set to prevent errors.
  
  > **Comments:**
  > **username_2**: 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.  
  > **username_1**: You're right, I clarified my answer.  
  
  ---
  
  