javascript - Laravel : Call API Update Database using Ajax -
i have laravel frontend calling api using javascript when onlick on image. having 1 issue, when user clicks fast on 3 img(in sequence:img1->img2->img3), id=img2, updates src correct image, didn't update flag in database
frontend blade:
<img src="/images/empty1.gif" id="img1" onclick="return changeflags(1,6)"/> <img src="/images/empty2.gif" id="img2" onclick="return changeflags(2,5)"/> <img src="/images/empty3.gif" id="img3" onclick="return changeflags(3,4)"/>
javascript:
<script type="text/javascript" charset="utf-8"> function changeflags(s9bid, position) { console.log("s9bid: "+s9bid); console.log("position: "+position); var url = "{{env('api_url')}}"; $.ajax({ method: "get", data: { id:s9bid,position:position }, url: url+"user/updateflag", timeout:5000, success: function(data){ if(data['success' == 0]){ return false; } var new_path = data['result']['item']['path']; document.getelementbyid(s9bid).src = new_path+"?" + new date().gettime(); }, datatype: "jsonp", }); } </script>
console log:
s9bid: 1 position: 6 s9bid: 2 position: 5 s9bid: 3 position: 4
api controller:
public function updateflag(request $request){ $item = items::find($request->id); $boxupdated = box::where('id',$request->id) ->update(['flag' => 1,'display_position' => $request->position,]); $result = [ 'item' => $item, ]; return response()->json(["status" => 1, "message" => ["success"], "result" => $result], 200)->setcallback($request->callback); }
box database after update:
id | name | flag | position --------------------------------- 1 | img1 | 1 | 6 2 | img2 | 0 | 0 3 | img3 | 1 | 5
box database expected result:
id | name | flag | position --------------------------------- 1 | img1 | 1 | 6 2 | img2 | 1 | 5 3 | img3 | 1 | 4
Comments
Post a Comment