Submit form with multiple values

Published 6 days ago by Laravelephant

This is a stupidly simple question but I'm stuck on it for some reason.

I have a form which is generated dynamically. It contains various fields which could be a textarea, a select, a radio button etc.

Each field has three hidden fields containing: list_id question_id and section_id.

These are pulled directly from the model and are showing up fine when I preview the question's source code.

An example field looks like this:


@if($question->question_type === 'Yes/No')
<select name="answer[]">
    <option value="yes">Yes</option>
    <option value="no">No</option>
</select>
<input type="hidden" name="list_id" value="{{$list->id}}">
<input type="hidden" name="section_id" value="{{$section->id}}">
<input type="hidden" name="question_id" value="{{$question->id}}">
@endif 

In my controller I'm using a foreach loop which grabs the 'answer' field correctly and inserts them as expected. But it renders the $list, $section and $question ids as the same across all the fields.

I know that the foreach isn't catching them, but where am I going wrong in my code?

Thanks

Best Answer (As Selected By Laravelephant)
ftrillo

@Laravelephant

Try this logic:

foreach($request->answer as $key => $answer) {
    $model = new Answer();
    $model->answer = $answer;
    $model->list_id = $request->list_id[$key];
    $model->section_id = $request->section_id[$key];
    $model->question_id = $request->question_id[$key];

    $model->save();
}

That will only work assuming that for each answer you're sending a list_id, a section_id, and a question_id. All in the same order.

tykus
tykus
6 days ago (337,510 XP)

You have the array [] notation on the answer field, but not on the hidden fields, so the answer is received as an array, while the hidden fields are not.

Laravelephant

Whoops!

Ok, so I have it pulling the array but maybe it's my controller logic that is funny?

foreach($request as $response) {
            $answer = new Answer;
            $answer->answer = $response->answer;
            $answer->list_id = $response->list_id;
            $answer->section_id = $response->section_id;
            $answer->question_id = $response->question_id;
            $answer->save();
        }

When I dd($request) I get this:

 "answer" => array:4 [▼
        0 => "yes"
        1 => "wada"
        2 => "yes"
        3 => "yes"
      ]
      "list_id" => array:4 [▼
        0 => "1"
        1 => "1"
        2 => "1"
        3 => "1"
      ]
      "section_id" => array:4 [▼
        0 => "1"
        1 => "1"
        2 => "5"
        3 => "5"
      ]
      "question_id" => array:4 [▼
        0 => "1"
        1 => "2"
        2 => "6"
        3 => "7"
      ]
    ]
ftrillo
ftrillo
6 days ago (12,140 XP)

If all your questions in the view have those hidden inputs with the same name, then It's only natural they all end up with the same values.

Shouldn't you send them as an array? Like this:

<input type="hidden" name="list_id[]" value="{{$list->id}}">
<input type="hidden" name="section_id[]" value="{{$section->id}}">
<input type="hidden" name="question_id[]" value="{{$question->id}}">

That's assuming all questions have those inputs.

Laravelephant

Hey @ftrillo - I have just implemented that but still having some issues pulling it on the controller end.

ftrillo
ftrillo
6 days ago (12,140 XP)

@Laravelephant

Try this logic:

foreach($request->answer as $key => $answer) {
    $model = new Answer();
    $model->answer = $answer;
    $model->list_id = $request->list_id[$key];
    $model->section_id = $request->section_id[$key];
    $model->question_id = $request->question_id[$key];

    $model->save();
}

That will only work assuming that for each answer you're sending a list_id, a section_id, and a question_id. All in the same order.

tykus
tykus
6 days ago (337,510 XP)

So... answer[0] with list_id[0] with section_id[0] with question_id[0], right?

foreach($request as $key => $response) {
    $answer = new Answer;
    $answer->answer = $response->answer;
    $answer->list_id = $request->list_id[$key];
    $answer->section_id = $request->section_id[$key];
    $answer->question_id = $request->question_id[$key];
    $answer->save();
}

EDIT what @ftrillo said above

Laravelephant

Thanks @tykus and @ftrillo that worked perfectly!

Just for reference, is there a better way of structuring this sort of form to avoid needing to use the array etc?

Please sign in or create an account to participate in this conversation.