doobe01
111
7
Laravel

Javascript variables to Laravel

Posted 1 month ago by doobe01

I have a script to get current geolocation. I want to pass the latitude and longitude variables to the database when my form is submitted.

I know that it probably must be done with AJAX but not sure how to go about it. Anyone have any tips or methods to get this done? I am new to programming and just getting my feet wet so sorry if this is easy. Been Googling for like 2 hours already. Just need something to point me in the right direction.

Javascript located in Create Blade Form

<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);               
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

    x.innerHTML = "Latitude: " + latitude + 
    "<br>Longitude: " + longitude;
}
</script>

Create Blade Form

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Create Journal Entry</div>

                <div class="panel-body">
                    <form class="form-horizontal" method="POST" action="{{ route('journal.store') }}">
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                            <label for="name" class="col-md-4 control-label">Title</label>

                            <div class="col-md-6">
                                <input id="title" type="text" class="form-control" name="title" value="{{ old('title') }}" required autofocus>

                                @if ($errors->has('title'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('title') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('body') ? ' has-error' : '' }}">
                            <label for="body" class="col-md-4 control-label">Journal</label>

                            <div class="col-md-6">
                                
                                <textarea nid="body" type="" class="form-control" name="body" rows="10" cols="30" value="{{ old('body') }}" required></textarea>

                                @if ($errors->has('body'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('body') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    Publish
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Controller

public function store()
    {

        Journal::create([
           'title' => request('title'),
           'body' => request('body'),
           'user_id' => auth()->id()
        ]);
     
        return redirect('journal');
    }

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.