forbesimba's avatar

Saving dynamic input field values

I would like to know how I can get values from dynamically generated input fields. So I pull questions from my database and from the questions I would want to get reponses from users of the service. Here is the code that I use to create the form.

                        @if( $q->image == '')
                          <p>Q{{$key+1}}. {{ $q->questions}}</p>
                          <div class="col-md-6">
									<input type="text" class="form-control" name="answer{{$key+1}}" placeholder="Type answer here ...">
								</div>
                        @else
                        <p>{{$key+1}}. {{ $q->questions}}</p>
                        <img id="showImage" src="{{ (!empty($q->image)) ? url('upload/question_images/'.$q->image) : url('upload/no_image.jpg')}}" alt="Question" class="rounded-circle p-0 bg-primary" width="120">
                        <div class="col-md-6">
									<!-- <label for="input1" class="form-label">Enter Option 1</label> -->
									<input type="text" class="form-control" name="image_answer" placeholder="Type answer here">
								</div>
                        @endif
                          
                          <?php 
                                $options = json_decode(json_decode(json_encode($q->options)),true);
                          ?>
                          <input type="text" name="question{{$key+1}}" value="{{$q['id']}}">

                        </div>
                    @endforeach
                      <div class="col-sm-12">
                        <hr>
                        <!-- <input type="text" name="index" value="{{ $key+1}}"> -->
                          <button type="submit" class="btn btn-primary" id="myCheck">Submit Test</button>
                      </div>
               </div>
              </form>

and below is the response from the submit button when I do a dd($request->all());

array:9 [▼ // app\Http\Controllers\ResponseQuestionsController.php:40 "exam_id" => "9" "_token" => "lK41Y7NyMYWvSgfoiNSTSDdtHE1QYDMM6ABpt7yF" "subject_id" => "1" "answer1" => "answer 1" "question1" => "1" "answer2" => "answer 2" "question2" => "2" "image_answer" => "answer 3" "question3" => "3" ]

0 likes
0 replies

Please or to participate in this conversation.