0

I'm currently trying to do the following: I have two input fields, like this:

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

                                <div class="col-md-6">
                                    <input id="plz" type="number" class="form-control" name="plz"
                                           value="{{$plz}}" autofocus required>
                                </div>
                            </div>

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

                                <div class="col-md-6">
                                    <input required id="city" type="text" class="form-control" name="city"
                                           value="{{$city}}">
                                </div>
                            </div>

So one for the postal-code and one for the city. What I want is, that when somebody enters a postal code, the city is automatically filled in (only need it for german cities). So for example somebody enters 70176 as postal code, then "stuttgart" should be automatically filled in. Propably we need AJAX for that? However I have no idea how to do it and where to get the data from.

Anybody can help me here?

PS: I'm using Laravel as PHP Framework if this might help

nameless
  • 1,483
  • 5
  • 32
  • 78

2 Answers2

2

Yes you need AJAX. I could probably only point you to the right direction.

[Backend]

  1. you need to create a City Controller and action to handle this request in your laravel app, which return a JSON Response. e.g.: http://localhost/api/city/{postalcode} your action handler must get this "postalcode" parameter and then match it with your database. (see step 4). once you found a match, return the response. https://laravel.com/docs/5.4/controllers#single-action-controllers

  2. This JSON response will contain the city: e.g.: json_encode(['city' => 'stuttgart']); https://laravel.com/docs/5.4/responses#json-responses

  3. You might need to obtain public data that you can use to match between PLZ and the city. https://www.aggdata.com/free/germany-postal-codes

  4. You have to then use CSV parser library or create one, and then match the postalcode with this data. https://github.com/parsecsv/parsecsv-for-php

[Frontend]

  1. use a JS library, I recommend JQuery for easy to install and small learning curve. "jQuery.get" is your friend here. https://api.jquery.com/jquery.get/

  2. so you have to "bind" onChange event on the PLZ input. onChange, it has to do a "jQuery.Get" and call this URL that you have setup on the [backend] part. http://api.jquery.com/bind/

  3. Once you got a response from the server, find the element you want to write the response, and put it there. e.g.: jQuery('.element').value(response.city);

I hope it can point you to the right direction.

r4ccoon
  • 3,056
  • 4
  • 26
  • 32
1

There are some ways to do this but the best way is to use Ajax.

in JQuery you would use something like this: (edit the [URL] and $form parts)

var $form = $("form:eq(0)");
$form.find("input[name=plz]").on('keyup', function(){
    $.get('[URL]?plz=' + $(this).val, function(result) {
        $form.find('input[name=city]').val(result);
    });
});

now in Server Side of the code make a page that searches through your database (you probably need a database of the names and postal codes for this) and prints the name of the city.

The Moisrex
  • 1,857
  • 1
  • 14
  • 16
  • Thanks for the answer. What would I have to change the URL part and the $form parts to? URL would just be localhost/path/plz=...on development and $form would be just the first form on the page like you did it right? – nameless Mar 26 '17 at 17:07
  • url depends on the backend of your site. for $form just give an id or something to the form or div or even body that contain the two inputs. – The Moisrex Mar 26 '17 at 17:29