Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chim3y's avatar

Add and Remove two fields dynamically on button click and how to retrieve it

Hello, I would like to request for any suggestion regarding these: add and remove two fields dynamically on button click and retrieving there value in controller.

i am able to add and remove the fields on the blade and also set up 'add-more' button using jquery:

I am trying to develop quiz application with quiz, question and answer model. Following is the view for create question and answer. Question(id, question, type, created_at, updated_at) and Answer(id, answer, question_id, created_at, updated_at).

<br/>
<div class="row">
<div class="col-sm-10 col-sm-offset-1">

<div class="row">
<div class="panel panel-primary" style="border: 0;">
<div class="panel-heading">
<div class="row">
<div class="col-sm-10">
<h2> <b>  {{ ucfirst(trans($type)) }} Question</b> </h2>
</div>
</div>
</div>


<div class="panel-body">
{!! Form::open(['files'=>'true','route'=>['admin.questions.type.store',$type]]) !!}

<div class="row">
<div class="col-sm-1"> </div>
<div class="col-sm-10">
{!! Form::textarea('question',null, ['class'=>'form-control']) !!}
</div>
<div class="col-sm-1"> </div>
</div>
<hr/>

<div class="row">
<div class="form-group">
<div class="col-sm-6 col-sm-offset-1">


<div id="items">

<input type="text" name="answer[]" class="form-control col-sm-2">

<input type="checkbox" name="is_correct" value="0"> is Correct

</div>

</div>
</div>
</div>
<br/>
<br/>

<div class="row">
<div class="col-sm-2 col-sm-offset-1">
<button type="button" class="btn btn-success" id="add">Add Answer</input>
</div>
</div>

<br/>
<br/>
<hr/>
<div class="row">
<div class="form-group">
<div class="col-sm-2 col-sm-offset-1">
{{Form::submit('Save', ['class'=>'btn btn-primary form-control', 'name'=>'save'])}}
</div>
</div>
</div>
<br/>
<br/>



</div>
</div>
</div>

</div>
</div>

<script type="text/javascript">
$(document).ready(function(){


//when the Add Field button is clicked
$("#add").click(function (e) {
//Append a new row of code to the "#items" div
$("#items").append(' <div>  <input type="text" name="answer[]" class="form-control"> <input type="checkbox" name="is_correct" value="0"> is Correct  <button  class="delete btn btn-danger">Delete</button></div>  '); });

$("body").on("click", ".delete", function (e) {
   $(this).parent("div").remove();



});

});

</script>

Following is the store controller for this question and answer:

   {

         
      $question=new Question;
      $question->question=$request->question;
      $question->type=$type;
      $question->save();
      
      $answer=new Answer;
   dd(array[$request->answer]);
   

   } 

i want to create answer with question_id, retrieved answer[] and is_correct[]. I am sorry, i don't know how to retrieve the two array fields: Answer[] and is_correct[]. Thank you for your time.

0 likes
2 replies
bobbybouwmann's avatar
Level 88

answer is an array so you can simply loop over it in your controller

foreach($request->answer as $answer) {
    dump($answer);
}
dd('Done');
1 like
chim3y's avatar

@bobbybouwmann you are right; i also wanted to add the value of is_correct and answer at the same time like is_correct[1] and answer[1] and then consequently others. I had to manually create 4 text input and checkbox. Thank you soo much.

Please or to participate in this conversation.