1

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:

<input type="hidden" class="questionThemeId" name="questionThemeId" value="{{ themeId }}">

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!

1 Answer 1

1

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?

2
  • 1
    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
    Commented Feb 1, 2017 at 10:58
  • Glad you got it sorted!
    – Brad Bell
    Commented Feb 2, 2017 at 6:32

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.