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

david001's avatar

Dynamically add input fields and save data to Database

I want to save the multiple data to database with autoincremented id (1,2,3...etc),not in same column. User can dynamically add input fields and finally click submit button to save data in database with different id(auto incremented id) for each.

my js to generate input field as many as user wants

<script>

$(document).ready(function(){
    
    $('#add').click(function(){
        
        var inp = $('#box');
        
        var i = $('input').size() + 1;
        
        $('<div id="box' + i +'"><input type="text" id="name" class="name" name="name' + i +'" placeholder="Input '+i+'"/><img src="remove.png" width="32" height="32" border="0" align="top" class="add" id="remove" /> </div>').appendTo(inp);
        
        i++;
        
    });
    
    
    
    $('body').on('click','#remove',function(){
        
        $(this).parent('div').remove();

        
    });

        
});

</script>
<div id="box">
<form action="{{url('add')}}" method="post">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<input name="name[]" type="text" id="name" class="name" placeholder="Input 1">
<a href="#" id="add">Add More Input Field</a>
<input type="submit" value="submit">
</div>

route:

Route::get('add','AddMoreController@create');
Route::post('add','AddMoreController@store');

controller:

public function create()
    {
        return view('add_more');
    }
public function store(Request $request)
    {
        $bio =  new Bio;
        $bio->details = $request->get('name');
        //dd($request->get('name'));
        $bio->save();
        return "Success";
    }

Please ,anybody who can help me

0 likes
15 replies
david001's avatar

on chnaging name[] to name only first input field value gets insert into database but not 2nd, 3rd...so on values.i need to save all values from input fields.

JasonS's avatar

I am not sure if I understand this correctly or not. What I think you are saying is that you want the user to be able to enter a list of names. Each name will save separately in the database. Is this correct?

So, they enter 1 name, create a second input box, then enter a second name and press save. All you want to do is save 2 names as separate rows?

In which case you will want to loop through the names.

public function store(Request $request)
{
    foreach($request->get('name') as $name) {
        $bio =  new Bio;
        $bio->details = $name;
        //dd($request->get('name'));
        $bio->save();
        return "Success";
    }
}

I need to look more into how eloquent works. On the surface this looks bad because you have a query in a loop. However, if you are only expecting a handful of names it would fine. If you are expecting hundreds or thousands of names then each request is going to do that many queries which is awful.

This post here covers bulk insert but it is pretty old. I would expect there would be a better way.

https://laracasts.com/forum/?p=649-bulk-insert-update/0

1 like
david001's avatar

@JasonS it is not working ,only first field value is save into database but second ,third field are not save in database

JasonS's avatar

In your javascript you have the following code name="name' + i +'". Changing that to name="name[]" would probably do the trick. At the moment I assuming the additional fields are being called name1, name2 etc

If not then add

var_dump($request->get('name'));

To see what is actually coming back when you submit the form.

bluelock's avatar

Try replacing:

<input name="name[]" type="text" id="name" class="name" placeholder="Input 1">

with:

<input name="name[0]" type="text" id="name" class="name" placeholder="Input 1">

and within your javascript, replace this:

<input type="text" id="name" class="name" name="name' + i +'" placeholder="Input '+i+'"/>

with this:

<input type="text" id="name" class="name" name="name[' + i +']" placeholder="Input '+i+'"/>

Then in your store() method, loop through the names, as @JasonS suggested, and save them one by one.

david001's avatar

@bluelock i followed your steps as well but it is not working,only first field value is saved in database,other fields value are not saved in database

TheBlueDragon's avatar

i have same thing in my app

i use the input like this

<input id="Answers[]" name="Answers[]" class="form-control" type="text" placeholder="" required> 
  • make all the inputs have same id and name with the [] so the request will be as an array

in my controller

$answers = $request->input('Answers');
  for($i=0;$i<count($answers);$i++){
    $answer = new Answers([
      'answer' => $answers[$i],
      'questions_id' => $question->id,
      'correct' => ($request->input('Answer_chk')[0]) == ($i+1) ? 1:0
    ]);
      $answer->save();
  }

** just i save it to variable and loop through it u can shortcut the code if you want but for me this is one work perfectly *** inside the loop you will make the logic normally as you save only one input to db and save it ^^

i hope its work with you i didnt try it with javascript but i think it will work fine


for your code it will be like this

<input name="name[]" type="text" id="name[]" class="name" placeholder="Input 1">

inside your controller

    for your code it will be like this 

<input name="name[]" type="text" id="name[]" class="name" placeholder="Input 1">

inside your controller public function store(Request $request) { $bios = $request->input('name'); for($i=0;$i<count($bios);$i++){ $bio = new Bio([ 'details' => $bios[$i], ]); $bio->save(); } }

thomaskim's avatar

@david001 @JasonS You were close. You shouldn't put a return statement in the foreach loop or it'll terminate the method on the first run. Put that outside the loop like this:

public function store(Request $request)
{
    foreach($request->get('name') as $name) {
        $bio =  new Bio;
        $bio->details = $name;
        $bio->save();
    }
    return "Success";
}
1 like
david001's avatar

it i still not woking, only first field data is saved in database but other input fields data not saved in database is something wrong in my js

webmasterkarthi's avatar

Did you checked whether you receive all entered names in field name i.e print_r($_POST['name']). If you receive all entered names in an array then you have to focus on the query else you need to focus on the html to pass all entered dynamic values. Let me know where the problem exactly. Either on the insert query or with the html post because i can't assume your current code as many were suggested right flow. So, telling us specifically where the problem would tip us to catch the point.

Ranjeet's avatar
Ranjeet
Best Answer
Level 2

This is you problemname="name' + i +'" you are requesting$request->get('name') not $request->get('name1'),$request->get('name2') ...

Change name="name' + i +'" to name="name[]"to get the name with $request->get('name')

change .appendTo(inp); to .appendTo($('#box form'));

mattverick's avatar

The end 'form' tag it is not in the proper html three position or is not present. Put the end form tag at the same three level of the open form tag. That worked for me

markdonnie's avatar

how about the update of dynamically add input fields in blade? thanks

Please or to participate in this conversation.