I am pretty new to JSON and ajax and don't know, how to get the JSON data from my request?
At the moment I have got two forms. One is for the search(a address) and the other one is for validating the data from the search form.
I get the data from openstreetmap nomination in JSON format for the first form(the address search) and I want to put the data from the search in the second form to validate the address. But I don't know, how to access the JSON data, to put them in the second form?
Here is my jsfiddle:
The Search is working on my machine, I guess, I did something wrong on jsfiddle with the ajax request.
My HTML:
<div class="col-md-8 col-md-offset-2 form-container">
<div id="map-adress"></div>
<div class="panel panel-default marker-panel">
    <div class="panel-heading">Create a post(Marker)</div>
    <div class="panel-body">  
        <div class="col-md-8 col-md-offset-4">
            <p>Insert the adress of the marker position<br>
                (You can move the marker on the map to the right position).</p>
        </div>
        <div class="form-group">
            <label for="findbox-adress" class="col-md-4 control-label find-label">Marker Location</label>
            <div class="col-md-6">
                <div id="findbox-adress"></div>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary user_marker_btn">
                    Validate
                </button>
            </div>
        </div>
    </div>
</div>
<div id="user_marker_adress" class="panel panel-default">
    <div class="panel-heading">Validate the marker adress</div>
    <div class="panel-body">
        <form class="form-horizontal" method="POST" action="{{ route('userposts.create') }}">
            {{ csrf_field() }}
            <div class="form-group">
                <label for="a_street" class="col-md-4 control-label">Street</label>
                <div class="col-md-6">
                    <input id="a_street" type="text" class="form-control" name="a_street" required>
                </div>
            </div>
            <div class="form-group">
                <label for="a_street_no" class="col-md-4 control-label">Street No.</label>
                <div class="col-md-6">
                    <input id="a_street_no" type="text" class="form-control" name="a_street_no" required>
                </div>
            </div>
            <div class="form-group">
                <label for="a_zip_code" class="col-md-4 control-label">Zip Code</label>
                <div class="col-md-6">
                    <input id="a_zip_code" type="text" class="form-control" name="a_zip_code" required>
                </div>
            </div>
            <div class="form-group">
                <label for="a_village" class="col-md-4 control-label">Village</label>
                <div class="col-md-6">
                    <input id="a_village" type="text" class="form-control" name="a_village" required>
                </div>
            </div>
            <div class="form-group">
                <label for="a_city" class="col-md-4 control-label">City</label>
                <div class="col-md-6">
                    <input id="a_city" type="text" class="form-control" name="a_city" required>
                </div>
            </div>
            <div class="form-group">
                <label for="a_state" class="col-md-4 control-label">State</label>
                <div class="col-md-6">
                    <input id="a_state" type="text" class="form-control" name="a_state" required>
                </div>
            </div>
            <div class="form-group">
                <label for="a_country" class="col-md-4 control-label">Country</label>
                <div class="col-md-6">
                    <input id="a_country" type="text" class="form-control" name="a_country" required>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-8 col-md-offset-4">
                    <button type="reset" class="btn btn-danger">
                        Clear
                    </button>
                    <button type="submit" class="btn btn-primary">
                        Next
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
My Javascript/jQuery:
$(document).ready(function(){
/************************************************************
Create Openstreetmap
************************************************************/
var map = new L.Map('map-adress', {zoom: 2, center: new L.latLng([24.61, -34.63]) });
map.addLayer(new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}));
/************************************************************
Create leaflet-search plugin properties
************************************************************/
var search = map.addControl( new L.Control.Search({
    container: 'findbox-adress',
    url: 'http://nominatim.openstreetmap.org/search?format=json&addressdetails=1&q={s}',
    jsonpParam: 'json_callback',
    propertyName: 'display_name',
    propertyLoc: ['lat','lon'],
    marker: L.marker([0,0], {draggable:'true'}).addTo(map).on('click', getLatLon),
    autoCollapse: false,
    collapsed: false,
    autoType: false,
    minLength: 2,
    autoResize: false,
    zoom: 18
    })
);
$( ".search-input" ).attr( "Placeholder", "No., Street, city, zip code, State and country" );
/* get Marker locataion(lat/lng) */
function getLatLon(e) {
    alert(this.getLatLng());
}
/************************************************************
Create User Marker(Adress) Form
************************************************************/
$(".user_marker_btn").click(function(){
    var searchString = $("#searchtext9").val();
    var searchArray = searchString.split(', ');
    console.log(searchArray);
    var JSONArray = $.parseJSON(search);
    console.log(JSONArray);
    $("#a_street").val(searchArray[1]);
    $("#a_street_no").val(searchArray[0]);
    $("#a_zip_code").val(searchArray[5]);
    $("#a_village").val(searchArray[2]);
    $("#a_city").val(searchArray[3]);
    $("#a_state").val(searchArray[4]);
    $("#a_country").val(searchArray[6]);
    $("#user_marker_adress").css( "display", "block" );
}); 
});
How can I access the JSON data from the request, to put them in the second form? At the moment I do it manually with the values from the input field. But I have got a problem, if the user type in the address backwards, or if there are no street number, then my script fails.
I don't know, on which variable or function I have to get the json from? The leaflet-search plugin confuse me.
 
    