0

In my Laravel-5.8, I passed data from controller to view using JSON.

public function findScore(Request $request)
{
    $userCompany = Auth::user()->company_id;
    $child  = DB::table('appraisal_goal_types')->where('company_id', $userCompany)->where('id',$request->id)->first();
   if(empty($child))
   {
       abort(404);
   }  
   $maxscore2 = 1;
    $maxscore = DB::table('appraisal_goal_types')->select('max_score')->find($child->parent_id);
    return response()->json([
        'maxscore' => $maxscore,
        'maxscore2' => $maxscore2
    ]);        
}

Route

Route::get('get/findScore','Appraisal\AppraisalGoalsController@findScore')
    ->name('get.scores.all');

View

<form  action="{{route('appraisal.appraisal_goals.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
 {{csrf_field()}}

 <div class="card-body">
 <div class="form-body">
 <div class="row">

  <div class="col-12 col-sm-6">
    <div class="form-group">
      <label class="control-label"> Goal Type:<span style="color:red;">*</span></label>
      <select id="goal_type" class="form-control" name="goal_type_id">
        <option value="">Select Goal Type</option>

        @foreach ($categories as $category)
        @unless($category->name === 'Job Fundamentals')
          <option hidden value="{{ $category->id }}" {{ $category->id == old('category_id') ? 'selected' : '' }}>{{ $category->name }}</option>

          @if ($category->children)
            @foreach ($category->children as $child)
            @unless($child->name === 'Job Fundamentals')
              <option value="{{ $child->id }}" {{ $child->id == old('category_id') ? 'selected' : '' }}>&nbsp;&nbsp;{{ $child->name }}</option>
            @endunless
            @endforeach
          @endif
          @endunless
        @endforeach
      </select>
    </div>
  </div>    

    <input type="text" id="max_score" value="max_score" class="form-control" >

    </form>

    <script type="text/javascript">
    $(document).ready(function() {
        $(document).on('change', '#goal_type', function() {
            var air_id =  $(this).val();

            var a = $(this).parent();

            console.log("Its Change !");

            var op = "";


            $.ajax({
                type: 'get',
                url: '{{ route('get.scores.all') }}',
                data: { 'id': air_id },
                dataType: 'json',      //return data will be json
                success: function(data) {
                    console.log(data.maxscore);
                    console.log(data.maxscore2);
                     $('#max_score').val(data.maxscore);
                },
                error:function(){

                }
            });
        });
    });
    </script>

When I click on the dropdown on change, I got these:

Console:

console diagram

textbox:

testbox diagram

I need the direct value to be displayed on the text and not the JSON object as shown in the diagram. For example, only the 75 in the textbox.

How do I get this resolved?

Thank you.

Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
mikefolu
  • 1,203
  • 6
  • 24
  • 57
  • Does this answer your question? [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – miken32 Mar 03 '20 at 21:42

2 Answers2

0

You're seeing this result because maxscore is an object. You can either reference maxscore.max_score from JavaScript when adding to the element, or you can alternatively look at your eloquent code. You can likely replace the ->find() with calls to ->where() and ->first() as you did with $child

megubyte
  • 1,549
  • 9
  • 15
0

You can access the inner elements using something like this, or at least have an idea , and then you can print the elemet you need from the array...

var selectedValue = max_score.options[max_score.selectedIndex].innerHTML;
var arreglo = selectedValue.split(' ');
var arreglo1 = arreglo[0];
AndrewDev
  • 51
  • 8