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'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:

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.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

You can use dot notation:

craft()->request->getPost('fields.notes');
share|improve this answer
    
nice answer selvin! –  Ben Croker Jun 27 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 Jun 27 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 Jun 27 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. –  Selvin Ortiz Jun 27 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... } –  Selvin Ortiz Jun 27 at 21:20

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:

$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.

share|improve this answer
    
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. –  Selvin Ortiz Jun 27 at 13:56
    
You're right, I clarified my answer. –  Victor In Jun 27 at 16:26

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.