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

gust's avatar
Level 1

How to Generate multiple forms based on input number?

I am making a form a to make a quiz and giving the user the ability to choose how many questions they want to have

quiz/create.blade.php

  otherformstuff...
   {{Form::label('question_count','Number of Questions:')}}
              {{Form::selectRange('number',1,20,['name'=>'question_count','id'=>'question_count'])}}
              <br>
              {{--{{Form::input('number','question_count',null,['min' => '1','max' => '20'])}}--}}

              {{--Question Form--}}
              {{Form::label('question','Question:')}}
              {{Form::text('question',null,['class'=>'form-control'])}}

              {{Form::label('choiceA','Choice A')}}
              {{Form::text('choiceA',null,['class'=>'form-control'])}}

              {{Form::label('choiceB','Choice B')}}
              {{Form::text('choiceB',null,['class'=>'form-control'])}}

              {{Form::label('choiceC','Choice C')}}
              {{Form::text('choiceC',null,['class'=>'form-control'])}}

              {{Form::label('choiceD','Choice D')}}
              {{Form::text('choiceD',null,['class'=>'form-control'])}}

              <br>
              {{Form::submit('Create Quiz')}}

Basedon the number that they choose I want to generate copies of this form on the same page. Not sure how to this and what library would I need to use. Is jQuery possible? or how to grab all of them once they get submitted into quiz.store function with some loop but if the choiceA,choiceB,choiceC,choiceD are gonna be replicated they won't be unique anymore

0 likes
2 replies
jasonlund's avatar

Creating the desired number of inputs can be done using a separate form before this one and a blade for loop or (more desirable) dynamically with Javascript/jQuery.

To get all of the choices, regardless of the number of inputs, name all of your inputs the same with a [] afterwards. For example:

        {{Form::label('choiceA','Choice A')}}
                {{Form::text('choice[]',null,['class'=>'form-control'])}}

                {{Form::label('choiceB','Choice B')}}
                {{Form::text('choice[]',null,['class'=>'form-control'])}}

                {{Form::label('choiceC','Choice C')}}
                {{Form::text('choice[]',null,['class'=>'form-control'])}}

                {{Form::label('choiceD','Choice D')}}
                {{Form::text('choice[]',null,['class'=>'form-control'])}}

This will be passed in your Request as an array of values (in this case, with four elements).

SaeedPrez's avatar

In case you really meant generate copies of this form, I would just like to tip that you're better off just generating input fields, all within the same form so they can be submitted together and at once.

Please or to participate in this conversation.