如果选中此复选框,则获取地理位置并使用ajax存储数据

问题描述

我有一个laravel刀片,我想在其中使用ajax存储一些值。 我希望当用户选中一个复选框然后获取其地理位置并立即使用ajax将其存储在db中时,请尝试使用此代码,但是它不起作用,这是控制器代码错误所在

public function visitclear(Request $request)
    
    {
        $customer = Customer::findOrFail($request->user_id);
        $customer->visit_clear = $request->visit_clear;
        $customer->ot_location = $request->ot_location;
        $customer->visit_date = date('Y-m-d H:i');
        $customer->save();
        return response()->json(['message' => 'User status updated successfully.']);
    }
           

javascript和html

<input autocomplete="off" class="form-control" type="text" id="otlocation" name="ot_location">
<input type="checkBox" data-id="{{ $customer->id }}" name="visit_clear" class="js-switch" {{ $customer->visit_clear == 1 ? 'checked' : '' }}>
<script type="text/javascript">
$(document).ready(function(){
    $(document).on('change','.js-switch2',function () {
        let visit_clear = $(this).prop('checked') === true ? 1 : 0;
        if ($(this).prop('checked') == 1) {
       $(this).closest('tr').addClass('visitclear');
      //ot get location
       function getLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
      } else {
        alert("Try any other browser");
      }
    }
    function showPosition(position) {
      $('[name="ot_location"]').val(position.coords.latitude+','+position.coords.longitude);
    }
    //$(document).ready(getLocation);
        } else {
           $(this).closest('tr').removeClass('visitclear'); 
        }
        let userId = $(this).data('id');
        var location = $("#otlocation").val();
        $.ajax({
            type: "GET",dataType: "json",url: '{{ route('users.update.visit_clear') }}',data: {'visit_clear': visit_clear,'user_id': userId,'ot_location': location},success: function (data) {
            console.log(data.message);
            }
        });
    });
});
</script>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)