[英]Retrieve information from Database Using Ajax with Laravel
I am just migrating from traditional PHP to Laravel framework using the MVC concept. 我只是使用MVC概念从传统的PHP迁移到Laravel框架。 I want to be able to fetch data from the database based on the selection made from a dropdown list box.
我希望能够基于从下拉列表框中进行的选择从数据库中获取数据。 I keep getting this error: " Sorry, the page you are looking for could not be found. (1/1) NotFoundHttpException ".
我一直收到此错误:“很抱歉,找不到您要查找的页面。(1/1)NotFoundHttpException”。
See my code: Route File: 查看我的代码:路由文件:
Route::get('/process-grpid','PagesController@processgrpid');
my Controller action code: 我的控制器动作代码:
public function processgrpid($pno){
$pno = $request->get('pno');
$det = Stock::where('itemName',$pno)->get();
return $det;
}
my view page: 我的查看页面:
<script type="text/javascript">
$(document).ready(function() {
$("select.partno").change(function(){
var selectedPno = $(".partno option:selected").val();
$.ajax(
{
type: "get",
url:"/processgrpid",
data:{pno:selectedPno},
success:function(data){
// var det = JSON.parse(data);
$("#desc").html('<input type="text" placeholder="Enter Item Description" class="form-control" name="descr" required="required"/>');
}
}
);
});
});
</script>
<table>
<tr>
<th>Item Code/Part NO:</th>
<td>
<select name="partno" class="partno form-control">
<option>Select PartNo</option>
<option value="N/A">N/A</option>
@foreach($pstock as $stock)
<option value="{{ $stock->itemName }}">{{ $stock->itemName }}</option>
@endforeach
</select></td>
<th>Description:</th>
<td id="desc"></td></tr>
<tr>
</table>
I don't know what am doing wrong. 我不知道在做什么错。 I have checked stackoverflow, but non of the soution meet my needs.
我已经检查了stackoverflow,但是没有一个满足我的需要。 All i wanted is to get the description of stock item from the database based on the dropdown list of partno.
我想要的只是基于partno的下拉列表从数据库中获取库存项目的描述。 Please, help me look and guide me accordingly .
请帮我看看并据此指导我。
You made a minor mistake in calling route through ajax request. 您在通过ajax请求调用路由时犯了一个小错误。
Change url:"/processgrpid"
to url:"/process-grpid"
. 将
url:"/processgrpid"
更改为url:"/process-grpid"
。
Reason: As you defined route as 原因:在您将路线定义为
Route::get('/process-grpid','PagesController@processgrpid');
So now public url will be process-grpid
instead of controller function processgrpid
. 因此,现在公共url将是
process-grpid
而不是控制器功能processgrpid
。
Also update controller function as: 还将控制器功能更新为:
public function processgrpid(Request $request) {
$pno = $request->get('pno');
$det = Stock::where('itemName', $pno)->get();
return $det;
}
you should pass $request
to your controller as below: 您应该将
$request
传递给控制器,如下所示:
public function processgrpid(Request $request){
$pno = $request->pno;
$det = Stock::where('itemName',$pno)->get();
return $det;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.