【发布时间】:2014-03-06 20:46:54
【问题描述】:
这里是 Ajax 新手问题:
我想使用 Ajax 从数据库中获取结果,并根据用户在表中输入的内容显示该行。
我想知道如何做到这一点:
这是我的控制器:
public function pricing()
{
$q = Input::get('term');
if($q && $q != ''){
$searchTerms = explode(' ', $q);
$query = DB::table('pricing');
if(!empty($searchTerms)){
foreach($searchTerms as $term) {
$query->where('unit', 'LIKE', '%'. $term .'%');
}
}
$results = $query->paginate(5);
$results->appends(array('term' => Input::get('q')));
dd($results);
return View::make('layouts.buyresults', compact('results'));
}
}
我的表单只是一个输入字段,名称为“term”和一个引导类。
雄辩的模型
<?php
class Pricing extends Eloquent {
protected $table = 'pricing';
}
因此基于此如何设置路线,我目前有:
Route::get('site/where-to-buy', 'HomeController@pricing');
我在表单中的操作是相同的,那么如何在不离开页面且不刷新的情况下实现这一点?所以用户输入一个数字,比如说 10,它们在数据库中匹配,然后 ajax 请求拉出该行并显示在一个表中。
提前致谢:)
【问题讨论】:
-
你有没有尝试过?您的问题在 StackOverflow 上被认为过于宽泛。
-
仅 dd($results);并试图传递给一个视图,我知道这是一个广泛的问题,但只是试图根据我的代码获取一些示例以供学习。