sara99
sara99

Reputation: 65

Retrieve data using Ajax Php laravel 5.3

According to my scenario when I select a doctor whose id is #slmc, I want to retrieve data relevant to the doctor I have chosen..

This is my blade File -> Admin/channel.blade

  <label id="lslmc" for="slmc" class="control-label">Choose a Doctor </label><br />
  <select class="form-control" id="slmc" name="slmc" onchange=" getPatient()" >
    <option value="">----Select the Doctor----</option>
    @foreach($Doctors as $Doctor)
      <option value ="{{$Doctor-> slmc}}">{{$Doctor->fname}} {{$Doctor->sname}}  </option>
    @endforeach
  </select>
</div>

<div id ="dd"></div>

Then this is my getPatient() function in the controller

public function getPatient($slmc){
        $patients = DB::table('channel')->where('slmc', $slmc)->get();
        return $patients;
}

This is the Ajax call in the script function.

function getPatient() {

    var patient = $("#slmc").val();

    $.ajax(
        {
            type: 'get',
            url: ('/Admin/channel/getPatient'),
            data: {'patients': patient},
            success: function (data) {
                $('#dd').html("");
                $('#dd').html(data);
            }
        }
    );
}

This is web.php

Route::get('/Admin/channel/getPatient{slmc}', 'channelController@getPatient' );

Is there something wrong with my Ajax call. I'm new to it.

Upvotes: 0

Views: 1726

Answers (2)

sara99
sara99

Reputation: 65

I was able to do it The ajax function goes like this

function getPatient() {
    var patient = $("#slmc").val();

    $.ajax({
        type: 'get',
        url: ('/Admin/channel/getPatient'),
        data: {'slmc': patient},
        success: function (data) {
            $('#dd').html("");
            var divHtml = "";
            divHtml += "<table class='table table-bordered table-hover'>";
            divHtml += "<tr>";
            divHtml += "<td>" + data[0].pname + "</td>";
            divHtml += "<td>" + data[0].address + "</td>";
            divHtml += "</tr>";
            divHtml += "</table>";
            $('#dd').html(divHtml);
            console.info(data);
        },
        error: function (data) {
            console.error(data);
        }
    });
}

And my controller function is

public function getPatient(Request $request) {
    $slmc = $request->get('slmc');
    $patients = \DB::table('channel')->where('slmc', $slmc)->get();
    return $patients;
}

web.php

Route::get('/Admin/channel/{slmc}', 'channelController@getPatient' );

Upvotes: 0

Rashedul Islam Sagor
Rashedul Islam Sagor

Reputation: 2059

Route :

Route::get('/Admin/channel/getPatient', 'channelController@getPatient' );

Controller :

ChannelController.php :

use Illuminate\Http\Request;
class ChannelController extends Controller
{

    public function getPatient(Request $request){
        $patients = \DB::table('channel')->where('slmc', $request->patients)->get();
        return $patients;
    }
}  

Upvotes: 1

Related Questions