php - Updating Database in Laravel 4 with AJAX Data -
i see there many similar questions out there 1 none have them have given me looking for.
i have jquery sortable list of records. when user moves record or down (so re-order records), jquery's .ajax() function should take data , pass laravel controller function called second(). function second() should take data and, each row, extract position (represented integer) , update 'order' column in database table. records should display in order on view.
i have working point data passed second() function:
route:
route::post( '/income-category/second', array( 'as' => 'income-category.second', 'uses' => 'incomecategorycontroller@second' ));
jquery:
$("#sortable").sortable({ update : function () { var serial = $('#sortable').sortable('serialize'); //split each li array item $("body").css("cursor", "progress"); $.ajax({ url: base_url + "income-category/second", type: "post", data: {serial: serial}, cache: false, datatype: 'json', success: function(info){ console.log(info); }, error: function(){ alert("there's error ajax"); } }); } });
controller (incomplete):
public function second(){ $orders = input::all(); foreach($orders $order){ $folge[] = $order; } return response::json($folge); }
after troubleshooting, got ajax output pop on console. turns out foreach loop represented above throw "invalid foreach argument" error because - i'm assuming - there 1 key in following object:
object {serial: "item[]=2&item[]=3&item[]=1&item[]=4&item[]=5"}
when have done in other, non-laravel projects, simple foreach($_post['item'] $item) worked. not.
html excerpt:
<tbody id="sortable"> @if(isset($_get['category'])) {{ form::open(array('route'=> array('income-category.update', $_get['category']), 'method'=>'put')) }} @foreach($categories $catid=>$catval) @if($_get['category'] == $catid) <tr id="{{ 'item_'. $_get['category'] }}"> <td>{{ form::text('category_name', $catval) }} <br>{{ $errors->first('category_name') }}</td> <td> {{ form::submit('save', ['category'=>$catid, 'class'=>'btn btn-success btn-sm', 'name' => 'submit_' . $_get['category'], 'title'=>'save']) }} {{ link_to('income-category', 'cancel', ['class'=>'btn btn-default btn-sm', 'title'=>'cancel']) }} </td> </tr> @else <tr> <td>{{ $catval }}</td> <td>{{ html::linkaction('incomecategorycontroller@index', '', ['category'=>$catid], ['class'=>'glyphicon glyphicon-pencil shadow pointer', 'title'=>'edit']) }} {{ form::button('', ['category'=>$catid, 'type'=>'submit', 'class'=>'glyphicon glyphicon-trash shadow pointer', 'name' => 'delete' . $_get['category'], 'title'=>'delete', 'onclick' => 'confirm("for going delete no matter pick.")']) }} </td> </tr> @endif @endforeach {{ form::close() }} ... ...
i confused beyond hope makes little sense.
Comments
Post a Comment