fill out a form automatically

0

In a TypeText input I look for a match by name within a table in a db of what the user types in that input. In the controller I receive the values of what the user writes ( $ term ) as follows:

public function jqueryWithAction(Request $request)
{
    $names = array();
    $term = trim(strip_tags($request->get('term')));
    $em = $this->getDoctrine()->getManager();

     $query = $em->createQuery(
            'SELECT ts
                FROM TestBundle:Test ts
                    WHERE ts.nombre LIKE :nombre'
        )->setParameter('nombre', "%".$term."%");
        $entities=$query->getResult();

    foreach ($entities as $entity)
    {
        $names[] = $entity->getNombre();
    }

    $response = new JsonResponse();
    $response->setData($names);

    return $response;
}

on the front side the function that sends the values ($ term) to the controller is the following:

$('.autocomplete').autocomplete({
        source: "{{ path('busqueda_jquery') }}",
        onSelect: function (suggestion) {
            alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
        }
    });

HTML

<div class="form-group txt">
<label class="col-sm-3 control-label">Nombre</label>
<div class="col-sm-6 col-md-6">
    {{ form_widget(form.nombre,{'attr':{'class':'autocomplete'}}) }}
</div>
</div>

<div class="form-group txt">
<label class="col-sm-3 control-label">Apellido</label>
<div class="col-sm-6 col-md-6">
   <input type="text" class="form-control">
</div>
</div>

Up to here the search is fulfilled, the question is that when finding this value I need to fill some other input (empty and without class) with the data belonging to the field name that you consult.

For example, fill one or more input automatically with the last names (to say the least) belonging to the name field that was consulted.

    
asked by matteo 31.07.2018 в 21:06
source

1 answer

0

Because you do not try like this:

$('.autocomplete').autocomplete({
    source: "{{ path('busqueda_jquery') }}",
    onSelect: function (suggestion) {
        alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
        $(".apellido").val(suggestion.data);
    }
});

HTML

<div class="form-group txt">
<label class="col-sm-3 control-label">Nombre</label>
<div class="col-sm-6 col-md-6">
    {{ form_widget(form.nombre,{'attr':{'class':'autocomplete'}}) }}
</div>
</div>

<div class="form-group txt">
<label class="col-sm-3 control-label">Apellido</label>
<div class="col-sm-6 col-md-6">
   <input type="text" class="form-control apellido">
</div>
</div>
    
answered by 31.07.2018 в 22:50