0

I have a code that changes the content of another select option based on the selection of another select option, but the issue is that when an option is picked from the first select option it takes time for the other to auto-populate sometimes it does not populate until users refresh the page and try again.

 var $ = jQuery;
        var dstvplans = {!! json_encode($dstvplans) !!};
        var gotvplans = {!! json_encode($gotvplans) !!};
        var startimesplans = {!! json_encode($startimesplans) !!};
       
          $(document).ready(function () {
          $("#product").change(function () {
              $('#plan').children('option').remove();
              var val = $(this).val();
              var toAppend='<option value="null">default</option>>';
              if (val == "BPD-NGCA-AQA") {
                $.each(dstvplans, (index, item) => {
                  toAppend+=`<option value=${item.id}>${item.name} ${item.price}</option>`;
                });
               
              } else if (val == "BPD-NGCA-AQC") {
                $.each(gotvplans, (index, item) => {
                  toAppend+=`<option value=${item.id}>${item.name} ${item.price}</option>`;
                });
              
              } else if (val == "BPD-NGCA-AWA") {
                $.each(startimesplans, (index, item) => {
                  toAppend+=`<option value=${item.id}>${item.name} ${item.price}</option>`;
                });
             
              } 
              $("#plan").append(toAppend)
          });
      });
</script>

note I am getting the content from my backend...here is my backend code

 public function index()
    {
        $cablenetworks = CableNetwork::all();
        $dstvplans = CablePlan::where('cablenetwork_id', 1)->get();
        $gotvplans = CablePlan::where('cablenetwork_id', 2)->get();
        $startimesplans = CablePlan::where('cablenetwork_id', 3)->get();
        $transactions = Transaction::where('user_id', Auth::id())
            ->where('payment_type', 'cable')->orderBy('created_at', 'DESC')->paginate(10);
        $gettoken = AirAdmin::all();
        foreach ($gettoken as $q) {
            $token = $q->token;
        }
        return view('resellerutility.index', compact('cablenetworks', 'dstvplans', 'gotvplans', 'startimesplans', 'transactions', 'token'));
    }

Please serious help is needed on this fast!

  • can you please add your html code and are you got any error then add that as well. – Senthurkumaran Jul 11 '20 at 09:43
  • Of course it's slow, look at the procedure that you are going through to get the data into that select box..... it takes probably X seconds to process all the data... I would use a special mysql view for that calculation and pull it from there – lewis4u Jul 11 '20 at 09:46
  • You are running lot of queries. I highly recommend to take a look at **how to optimize** https://stackoverflow.com/questions/60015018/laravel-use-mysql-indexing/60015685#60015685 – Digvijay Jul 12 '20 at 06:28
  • i fixed it, by moving the jquery script to the top of the page, thanks for your time – Adewale Charles Jul 13 '20 at 19:09

0 Answers0