ERaufi
ERaufi

Reputation: 1663

clone table via jquery and insert it's data to database

hello guys I'm new to laravel.I want to clone my table and add multiple entries after clicking submit button and it should save all data to database

The problem that I'm facing is when I click submit button it only saves one record instead of all records

my view

        function addRow() {
        //copy the table row and clear the value of the input, then append 
 the row to the end of the table
        $("#formTable tbody tr:first").clone().find("input").each(function 
  () {
            $(this).val('');
        }).end().appendTo("#formTable");
        $("#removeclone").removeAttr("disabled");

    };
</script>
<div id="Home" style="width: 60%; margin: 0px auto;"></div>

<form method="post" action="insertrequest">
    @csrf
    <table id="formTable" >
        <thead>
            <td>Quantity</td>
            <td>Unit</td>
            <td>Description</td>
            <td>Remarks</td>
        </thead>
        <tr>
            <td><input type="text" name="quantity" id="quantity"></td>
            <td><select name="units_id" id="units_id">
                    @foreach($unit as $units)
                        <option value="{{$units->id}}">{{$units->unit_name}}</option>
                    @endforeach
                </select></td>
            <td><input type="text" name="description" id="description"></td>
            <td><input type="text" name="remarks" id="remarks"></td>
        </tr>
    </table>
    <input type="submit" value="submit">
</form>
<input value="addrow" type="button" id="addrow" onclick="javascript: addRow();">

My Route

Route::Post('insertrequest','Home@insertrequest');

and My controller

    public function insertrequest(Request $requset)
{
    $quantity[]=$requset->input('quantity');
    $units_id[]=$requset->input('units_id');
    $description[]=$requset->input('description');
    $requster='1';
    $remarks[]=$requset->input('remarks');
    for($i = 0; $i < count($quantity); $i++)
    {
        DB::insert('insert into procurment_requests (quantity,units_id,description,`requster`,remarks) 
        values(?,?,?,?,?)',
            [$quantity[$i],$units_id[$i],$description[$i],$requster[$i],$remarks[$i]]);
    }


}

enter image description here

Upvotes: 0

Views: 687

Answers (1)

Watercayman
Watercayman

Reputation: 8178

You've got a number of things that are preventing all the records from being saved. When you clone the row, you are cloning a single option select element. The element you are cloning, like the first, will only allow one answer to go through to the server.

Further, when you clone, I believe you are cloning a new select box, but with the same name and id into a new row. The submit is only going to see one of these. (Plus the dom is going to have fits).

I don't think this will work given your current architecture. I would suggest possibly cloning the options of the single select box instead of the whole row. Search SO for easy way to do that. Then, make it a multi-select ( <select name="units_id[]" multiple...etc> ).

The user can then click as many of the options as he wants - and they will be sent through as an array.

On your server side, change

$units_id[]=$requset->input('units_id');

to:

$units_ids=$requset->input('units_id');

The way you have it will only ever give you one entry, not arrays - it doesn't work that way. You then have an array inside that unit_ids variable that you can use.

Alternately, you may consider the multi-row approach, but with different select names and ids as well as totally different forms so they don't clash.

Not sure if you can work with this slightly different way of doing this, but I think you'll tear your hair out trying to get the other way to work.

HTH

Upvotes: 1

Related Questions