<blockquote>
  <p>That makes sense, because getPost always gets the last post (I think).</p>
</blockquote>

<p>If you've got multiple form inputs with the same name, then the last one in is probably the value that will get sent over POST to the controller.</p>

<p>You could make sure your themeId name inputs are unique by appending a unique string or some such to them, then parsing through all of the submitted values on the PHP side, perhaps?</p>

CC BY-SA 3.0
2017-02-01T10:58:59.697Z
Thanks for the support ;). I managed to get it working: I added an id to each questionThemeId `{{ id }}-questionThemeID` which is the place wherein the question is. I loop through the questions with a foreach $key loop and get the post with `"{$key}-questionThemeId"`.
sanderfish
CC BY-SA 3.0
2017-02-02T06:32:54.453Z
Glad you got it sorted!
Brad Bell
CC BY-SA 3.0
2017-01-31T19:07:55.207Z
2017-01-31T19:07:55.207Z
Brad Bell
<p>I'm working on a Craft CMS plugin. I'm making a form that consists of multiple themes and questions. The structure is supposed to be as follows:</p>

<pre><code>• Form
  • Theme
    • Question
    • Question
    • Question
  • Theme
    • Question
    • Question
</code></pre>

<p>Questions should be “attached” to the form with the formId, but also to their respective themeId. This kinda works when there’s just one theme, but as soon as I save a question in a second or third theme, all questions get the last theme id.</p>

<p>I’ve added a hidden field to each question which looks like this: </p>

<p><code>&lt;input type="hidden" class="questionThemeId" name="questionThemeId" value="{{ themeId }}"&gt;</code></p>

<p>Then in my Service I do the following request and add the themeId to the question.</p>

<pre><code>$themeIdForQuestion = craft()-&gt;request-&gt;getPost("questionThemeId");
$question-&gt;themeId = $themeIdForQuestion;
</code></pre>

<p>This does add the themeId to the question, but once there is a question in the second theme, all questions get the themeId of the last theme. That makes sense, because getPost always gets the last post (I think). How would I get post of the questionThemeId depending on the question?</p>

<p>I'm open to other solutions too, because this does feel slightly hacky.</p>

<p>Thanks!</p>

<plugin-development><templating><php><request>
How to get corresponding Post in Craft request?
# How to get corresponding Post in Craft request?

(Asked by: username_0 on 2017-01-31)

I'm working on a Craft CMS plugin. I'm making a form that consists of multiple themes and questions. The structure is supposed to be as follows:
• Form
  • Theme
    • Question
    • Question
    • Question
  • Theme
    • Question
    • Question

Questions should be “attached” to the form with the formId, but also to their respective themeId. This kinda works when there’s just one theme, but as soon as I save a question in a second or third theme, all questions get the last theme id.
I’ve added a hidden field to each question which looks like this: 

Then in my Service I do the following request and add the themeId to the question.
$themeIdForQuestion = craft()->request->getPost("questionThemeId");
$question->themeId = $themeIdForQuestion;

This does add the themeId to the question, but once there is a question in the second theme, all questions get the themeId of the last theme. That makes sense, because getPost always gets the last post (I think). How would I get post of the questionThemeId depending on the question?
I'm open to other solutions too, because this does feel slightly hacky.
Thanks!

**Tags:** plugin-development, templating, php, request


---

## 1 answers

### Answer by username_1 on 2017-01-31. Score: 1 | Accepted answer

> That makes sense, because getPost always gets the last post (I think).

If you've got multiple form inputs with the same name, then the last one in is probably the value that will get sent over POST to the controller.

You could make sure your themeId name inputs are unique by appending a unique string or some such to them, then parsing through all of the submitted values on the PHP side, perhaps?

> **Comments:**
> **username_0**: Thanks for the support ;). I managed to get it working: I added an id to each questionThemeId `{{ id }}-questionThemeID` which is the place wherein the question is. I loop through the questions with a foreach $key loop and get the post with `"{$key}-questionThemeId"`.  
> **username_1**: Glad you got it sorted!  

---

