使用 Ajax Php laravel 5.3 检索数据
Retrieve data using Ajax Php laravel 5.3
根据我的情况,当我select一个ID为#slmc的医生时,我想检索与我选择的医生相关的数据..
这是我的 blade 文件 -> 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>
那么这是我在控制器中的getPatient()
函数
public function getPatient($slmc){
$patients = DB::table('channel')->where('slmc', $slmc)->get();
return $patients;
}
这是脚本函数中的Ajax调用。
function getPatient() {
var patient = $("#slmc").val();
$.ajax(
{
type: 'get',
url: ('/Admin/channel/getPatient'),
data: {'patients': patient},
success: function (data) {
$('#dd').html("");
$('#dd').html(data);
}
}
);
}
这是web.php
Route::get('/Admin/channel/getPatient{slmc}', 'channelController@getPatient' );
我的 Ajax 通话有问题吗?我是新手。
路线:
Route::get('/Admin/channel/getPatient', 'channelController@getPatient' );
控制器:
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;
}
}
我做到了
ajax 函数是这样的
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);
}
});
}
而我的控制器功能是
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' );
根据我的情况,当我select一个ID为#slmc的医生时,我想检索与我选择的医生相关的数据..
这是我的 blade 文件 -> 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>
那么这是我在控制器中的getPatient()
函数
public function getPatient($slmc){
$patients = DB::table('channel')->where('slmc', $slmc)->get();
return $patients;
}
这是脚本函数中的Ajax调用。
function getPatient() {
var patient = $("#slmc").val();
$.ajax(
{
type: 'get',
url: ('/Admin/channel/getPatient'),
data: {'patients': patient},
success: function (data) {
$('#dd').html("");
$('#dd').html(data);
}
}
);
}
这是web.php
Route::get('/Admin/channel/getPatient{slmc}', 'channelController@getPatient' );
我的 Ajax 通话有问题吗?我是新手。
路线:
Route::get('/Admin/channel/getPatient', 'channelController@getPatient' );
控制器:
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;
}
}
我做到了 ajax 函数是这样的
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);
}
});
}
而我的控制器功能是
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' );