JoewyZN's avatar

Select subcategories from category id using Ajax, keep getting error 500

Hi .. i have a the following

My View

@extends('app')
@section('page_title','Add New Stock')
@section('content')
<div class="col-md-12">
    <div class="card">
        <div class="card-header" data-background-color="green">
            <h4 class="title">Stock</h4>
            <p class="category">@yield('page_title')</p>
        </div>
        <div class="card-content">
            <!-- Button trigger modal -->
            <button class="btn btn-success" name="add" id="add" data-toggle="modal" data-target="#ModalAddStock">
              Add New Stock
            </button>
            <button class="btn btn-warning" name="update" id="update" data-toggle="modal" data-target="#myModalUpdate">
              Update Stock
            </button>
            <button class="btn btn-info" name="edit" id="edit" data-toggle="modal" data-target="#ModalEdit">
              Edit Stock
            </button>
        </div>
    </div>
</div>  
 <a href="#" id="try" data-link="{{ url('/test') }}">Try</a>
@endsection

@section('scripts')

<!--<script src="{{url('/')}}/assets/js/populate.js" type="text/javascript"></script>-->

<!-- Modal Core -->
<div class="modal fade" id="ModalAddStock" tabindex="-1" role="dialog" aria-labelledby="ModalAddStockLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="ModalAddStockLabel">Add New Stock</h4>
      </div>
      <div class="modal-body">
            <div class="row">
              <form role="form" method="post" action="{{url('/ajax/populate/subcategories')}}" id="form-create">
                <div class="col-md-6">
                    <div class="form-group label-floating">
                        <label class="control-label">Title</label>
                        <input type="text" name="title" required class="form-control" >
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group label-floating">
                        <label class="control-label">ID</label>
                        <input type="text" name="id" required class="form-control" >
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group label-floating">
                        <label class="control-label">Price</label>
                        <input type="text" name="price" required class="form-control" >
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group label-floating">
                        <label class="control-label">Quantity</label>
                        <input type="text" name="quantity" required class="form-control" >
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-4">
                    <label class="control-label">Category</label>
                    <select class="form-control" id="category" required name="category">
                        @foreach($categories as $category)
                            <option value="{{$category->id}}">{{$category->title}}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-4">
                    <label class="control-label">Subcategory</label>
                    <select class="form-control" id="subcategory" required name="subcategory">
                        <option value="0">choose a category</option>
                    </select>
                </div>
                <div class="col-md-4">
                    <label class="control-label" >Brand</label>
                    <select class="form-control" required name="brand" id="brand">
                        @foreach($brands as $brand)
                            <option value="{{$brand->id}}">{{$brand->title}}</option>
                        @endforeach
                    </select>
                </div>
            </div>              
            <div class="clearfix"></div>
            <div id="response" name="response"></div>
            <input type="hidden" name="_token" value="{{ csrf_token() }}"/>  
      </div>
      <div class="modal-footer">
          <div id="loading" name="loading"></div>
        <button name="add_stock" id="add_stock" type="submit" class="btn btn-info btn-simple">Save</button>
      </div>
      </form>
    </div>
  </div>
</div>  

<script type="text/javascript">

$(document).ready(function()
  {
    $("#form-create").on('submit',function(e)
    {
      e.preventDefault();
        $.ajax({
              headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
              url: "{{url('/ajax/populate/subcategories')}}",
              type: 'POST',
              dataType: 'json',
              data: {},
              success: function (data, textStatus, xhr) {

                  console.log(data);
              },
              error: function (xhr, textStatus, errorThrown) {

                  console.log('PUT error.');
              }
          });
      });  
  });

</script>

@endsection


My Route

Route::post('ajax/populate/subcategories','AjaxController@populate_subcategories');

My Controller


<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

use Illuminate\Http\Request;

class AjaxController extends Controller {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function populate_subcategories(Request $request)
    {
        if($request->ajax()){
            $states = DB::table('subcategories')->where('parent',$request->id_country)->pluck("title","id")->all();
            $data = view('ajax-select',compact('states'))->render();
            return response()->json(['options'=>$data]);
        }
    }

}

i have tried almost everything.. i still keep getting the same error 500 on change.. its driving me nuts! .. please help.. the thank you's will come later..

0 likes
3 replies
tykus's avatar

You do not appear to be sending any data with the request?

You can check your browser's DevTools to see what the error details - under Network, click on the (red) failing request and see the Response

1 like
JoewyZN's avatar

hi tykus, for now i just wanna successfully hit the route and get whatever data i can get.. after i achieve that then i can look into sending data...

tykus's avatar

So... your query expects a id_country in the request, but you're not sending it? Ok.

In any case, the tip about checking your browser's dev tools will help identify the reason for the 500 error.

Please or to participate in this conversation.