Complete form without reloading the page

0

I'm trying to implement a page where the values of a table can be changed in the database of a system.

When I select the name of the people listed, I call the function ola through onchange , which is working perfectly, but when trying to update the values registered in inputs of form , I can not. / p>

This is my role:

function ola(val){
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    })
    $.ajax({
        url:'editarInstalador',
        data:{pid:val},         
        type:"POST",
        data:{pid:val},
        success:function(data){

        },
        error:function(data){

        },
    });
    return false;
}

My route:

Route::post('editarInstalador', 'instaladorController@edit');

The function in the controller:

public function edit()
{
    $json = array();
    $idPessoa = request("pid");
    $instaladorDados =  DB::select("SELECT * FROM public.tblInstalador WHERE inst_id = {$idPessoa}");
    foreach ($instaladorDados as $var) {
        $json[]= array(
                'nomee' => $var->inst_razaosocial,
                'nomeFantasia' => $var->inst_nomefantasia,
                'cnpj' => $var->inst_cnpj,
            );
    }
    return response()->json($json);
}

I am able to perfectly call controller , do the search in the database and as an answer I have in response of the browser:

[{"nomee":"nomeCadastrado","nomeFantasia":"daniel franca","cnpj":"cnpj cadastrado"}]  

My problem is that first of all, in% with% of where I call the route, it literally always enters $.ajax , never enters error .

My second question is: how can I pass this response to success and fill view with this data?

    
asked by anonymous 22.10.2018 / 15:30

1 answer

1

Resolving your problem of falling into error soon when sending data ... First change your route to:

Route::post('editarInstalador', ['as' => 'editar.editarInstalador', 'uses' => 'instaladorController@edit']);

Soon after updating in ajax to:

...
$.ajax({
    url:"{{ route('editar.editarInstalador')}}",
    data:{pid:val},     
...

At first your Controller is correct, how to return json to ajax , you only need to treat return as necessary, check how response arrives in callback co_de % with success , a tip I can give is about console.log(data) method:

public function edit(Request $request)
{
    $idPessoa = $request->only("pid");
    $instaladorDados =  DB::select("SELECT * FROM public.tblInstalador WHERE inst_id = {$idPessoa}");
    foreach ($instaladorDados as $var) {
        $json[]= array(
                'nomee' => $var->inst_razaosocial,
                'nomeFantasia' => $var->inst_nomefantasia,
                'cnpj' => $var->inst_cnpj,
            );
    }
    return response()->json($json);
}

Ps: Consider using Controller instead of fetching data with Models simple as well.

On return handling you can access the returned objects by looking for the position and attribute of each item. The most crude and rustic way is:

<input id="inputNome" name="inputNome">

$('#inputNome').val(data[0].nomee);

If you have more than one result in Query Builders of returned objects, consider using a array loop, consider also the possibility of using each in return JSON.parse()

    
22.10.2018 / 16:00