Complete form without giving Reload on page

Asked

Viewed 423 times

0

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

When selecting the names of the people listed, I call the function ola through the onchange, which is working perfectly, but when trying to update the values registered in inputs of form, can’t.

This is my job:

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’m getting the perfect call controller, do the research in the bank and as an answer I have in the response browser:

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

My problem is that first, at $.ajax where I call the route, he literally always enters the error, never gets into the success.

My second question is: how can I pass this answer to the view and fill in the inputs with this data?

1 answer

1


Solving your problem of falling in error soon when sending the data... First change your route to:

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

Soon after update in ajax to:

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

At first his Controller is correct, the way to return the json to the ajax, you only need to treat the return in any way necessary, check how the answer arrives in the callback of success with the console.log(data), a tip I can give is on the method of Controller:

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 the Models instead of searching the data with Query Builders plain as that.

On the return manipulation you can access the returned objects by searching for the position and attribute of each item. The most crude and rustic form is:

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

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

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

  • Thanks for the help friend, the route issue worked perfectly and now it enters the Success correctly. I got a response on the console: [{... }] 0: {name: "filename", fileName: "Filename", cnpj: "cnpj registered"} length: 1 proto: Array(0) Please give me an example of how to play these values for inputs? Again thank you for your help

  • I decided using the code: Success:Function(data){ $("#test"). attr({value:data[0].nomee}); }, Again thank you for your attention. Att.

  • I just edited the answer, but that’s exactly what you commented

Browser other questions tagged

You are not signed in. Login or sign up in order to post.