Hello, I see what you are trying to do here. The reason your approach does not work is that the piece of javascript that is executed on submit will in fact run the submit() from all forms, but you are not sending it through any ajax request so the JS runs through all the submit quickly and the last form submit is the one that goes through all the way.
Here is another approach that you may try. Instead of sending multiple forms, you could send one form and use the [] syntax for the name attributes within your <input> tags. Here is an example:
<input type="text" name="person[0][first_name]" value="Clark">
<input type="text" name="person[0][last_name]" value="Kent">
<input type="text" name="person[1][first_name]" value="Bruce">
<input type="text" name="person[1][last_name]" value="Wayne">
...
This way, you wouldn't need the javascript code, nor would you need multiple <form>, just one. Your backend will have to change, as the data you receive will be put into an array, so a loop is in order.
public function store(Request $request)
{
foreach($request->person as $person) {
App\Test::create($person);
}
return redirect()->back();
}
Note that the above code is not safe, you will need to make sure that all fields are valid and that you do not have any mass assignment issues.
Hope this helps.
public function store(Request $request){
$input = Input::all();
$studentdata = $input['student_id'];
$coursedata = $input['c_id'];
$allOrder = array();
foreach ($coursedata as $key => $val) {
$do = new DO;
$do->course_id = $input['c_id'][$key];
// others input
foreach ($studentdata as $key => $val) {
//all input request data
}
$do->save();
}
}
Let me know if you face any other problem thanks
Jeremy Michel, in your
foreach($request->person as $person) {
App\Test::create($person);
}
When I create a person is there a way to pass in some default value in every person that is created. If yes can you please tell me. And BTW thanks for the reply it helped a lot.
Of course, I used the create() method in my example as a shorthand version. You could definitely do
foreach($request->person as $person) {
$tests = new \App\Test([
'first_name' => $person['first_name'],
'last_name' => $person['last_name'],
'foo' => 'bar' // Example of default value
]);
$tests->whatever = "foo"; // Another example, basically, anything you want
$tests->save();
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community