Fill in input according to other input response

Asked

Viewed 527 times

1

Good afternoon, I have the following code in my locker:

  <div class="form-group col-sm-6">
        <label class="control-label">{{ trans('app.select_client')}} <span class="text-danger">*</span></label>
            <select ng-model="cliente" class="form-control" name="cliente" id="cliente" required ng-init="cliente = '{{ old('cliente') }}'">
            <option value="">{{ trans('app.select_client')}} </option>  
                @foreach($clientes as $data)
                     <option value="{{$data->id}}">{{$data->nomeCliente}}</option>
                @endforeach
           </select>

  </div>


<div class="form-group col-sm-12">
     <label class="control-label" >{{ trans('app.for')}}<span class="spancolor">*</span> </label>
     <input type="text" class="form-control" value="" id="contato" ng-model="contato" name="contato" ng-init="contato='{{ old('contato') }}'" placeholder="{{ trans('app.contato')}}" required>     
</div>

<div class="form-group col-sm-12">
    <label class="control-label" >{{ trans('app.cc')}}<span class="spancolor">*</span> </label>         
    <input type="text" class="form-control" value="" id="cc" ng-model="cc" name="cc" ng-init="cc='{{ old('cc') }}'" placeholder="{{ trans('app.cc')}}" required>                            
</div>

I need to make the input id="contact" display the email of the selected client in select id="client"

My function in the controller

function getContatos(Request $request){

        $value = $request->get('value');
        $contatos = DB::table('clients')->select('contatos')->where('id', $value)->get();
        $cc = DB::table('clients')->select('cc')->where('id', $value)->get();
        echo $contatos;
        echo $cc;

        //return view('dashboard.dashboard_user',compact('contatos','cc')); 


    }

When I put the error Return What I need to return and show in the view is $contacts and $cc

$('select[name=cliente]').change(function () {

    associaInput();
  var value = $(this).val();
  var _token = $('input[name="_token"]').val();
    $.ajax({
        url:"{{ route('cliente.getContatos') }}",
        method:"POST",
        data:{value:value, _token:_token},
        success:function(result){
            alert("success");
            $("#contato").append($('#contato').val(value));
        }

    })
});

view function

Route::post('getContatos', 'DashboardController@getContatos')->name('cliente.getContatos');

Route

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Client extends Model
    {
        protected $table = 'clients';
        protected $fillable = ['nomeCliente', 'contatos', 'cc', 'obscontatos', 'horarioInicial', 'horarioFinal', 'observacao', 'analistapreferencial', 'servicos'];
        protected $casts = [
             'cc' => 'array', 'obscontatos' => 'array', 'contatos' => 'array',
        ];

    }

Model de Clients

  • 1

    In vdd I think you will have to use AJAX to get the client name and do this if with javascript.

  • Have you ever used auxiliary variable? always save the previous name in the course of cilclo ?

  • Good morning, could explain better the pq to have the second foreach running the same array. I couldn’t understand, maybe explaining better I can help you.

  • In the first input I go through the array to get {{$data->client name}} and in the second the value of {{$data->contacts}}

  • Only there in the second input I need to compare {{$data->client name}} with the value of the first input.

  • @Viniciusdejesus the client name I can already pick up and show in javascript, but I don’t know how I will access the $clients array in java script to scan.

  • @Lorena in my reply wanted you to post how comes the return of the request, in the console.log() I put

  • @Joanmarcos jquery.js:10261 GET http://localhost/monitoring/public/getContacts? value=1&_token=wfzBV0Yzb2r2E6GPuTVeWJjLBhibMVPf7ozT7HS 500 (Internal Server Error)

  • @Lorena has a way to log the error message ? to see what really went wrong

  • @Joanmarcos {,... } Exception: "Illuminate Database Queryexception" file: "C: xampp htdocs vendor monitoring Laravel framework src Illuminate Database Connection.php" line: 664 message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'contacts, cc' in 'field list' (SQL: select contatos, cc from clients Where id = 3)" trace: [{,... }, {,... }, {,... }, {,... },...]

  • @Joanmarcos I don’t understand because, the connection with the bank and the columns are correct.

  • put your clients model :D

  • @Joanmarcos Inseri na descrição da pergunta.

  • Let’s go continue this discussion in chat. @Lorena

Show 9 more comments

2 answers

2


In fact it is quite quiet to carry out such barbarism, come on the/


Ajax

$(document).on("change", "#cliente", function () {
 var value = $(this).val();
 var _token = $('input[name="_token"]').val();
 $.ajax({
     url:"{{ route('cliente.getContatos') }}",
     method:"get",
     data:{value:value, _token:_token},
     success:function(result){
       console.log(result)//exibir o resultado da pesquisa no controller
       $("#contato").val(result[0]['contatos']);
       $("#cc").val(result[0]['cc']);
     }
  })
});

Route

Change of post for get (get pq you will only query the database and no change or insertion)

Route::get('getContatos', 'DashboardController@getContatos')->name('cliente.getContatos');

Controller

public function getContatos(Request $request) 
{
   $value = $request->get('value');

   $dadosCliente = DB::table('clients')
                       ->select('contatos','cc')
                       ->where('id', $value)
                       ->get();

  return response()->json($dadosCliente);
}

0

I suggest you use an ajax using the event onChange() in select of clients. Below is an example:

<div id="respostaAjax"></div>
<script type="text/javascript">
    $("#cliente").change(function(){
        type:"POST",
        data: $(this).val(),
        url: 'link/que/pesquisa/os/contatos/para/este/cliente.php'
        success: function(data){
            $("#respostaAjax").empty();
            $("#respostaAjax").append(data);
        }
    })
</script>

After adding a <div> who had received the response of the ajax which in this case will be a select containing the contact details of the client you selected earlier. Any doubt I edit the answer to adapt to your need. Explain your need better so others can help you too.

  • I did it this way, but the value that goes to my response input is the value of select id="client" and no, the mail of the client as wish, the client’s email is already being returned in the browser, just could not return it to view yet.

Browser other questions tagged

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