我有一个表单,用于对抛到视图中的数据进行实时筛选。我刚刚了解了序列化(),据我所知,它接受所有表单数据,并将其转换为可以传递到ajax函数中的data: field的字符串。
所以我的表单在伪代码中是这样设置的:
<?php foreach (city that's returned from a distinct select statement): ?>
<?php echo form_checkbox('city-'.$i++, etc...) ?>这使:
<input type="checkbox" id="city-1" name="city-1" class="filter city-filter">
<input type="checkbox" id="city-2" name="city-2" class="filter city-filter">
<input type="checkbox" id="city-3" name="city-3" class="filter city-filter">目前,我的模型有一个函数,用于接收这些数据并动态构建sql语句,如下所示:
public function filter_assocaitions($cities, $class, $types)
{
$this-db->select()->from('Associations')->where('Deleted', 0);
if(isset($cities))
{
$this->db->where('City', any of the city or cities that get passed from ajax);
}
}我对CI和php还不太熟悉,无法让ajax与我的控制器挂钩来编写适当的select函数。
因为当新记录被添加到我的表中时,会有新的不同的城市名称,因此将提供更多的输入。我是否可以使用序列化将所有这些整齐地封装在一起,以传递到ajax,如果是这样的话,我如何在CI模型中使用它来使用数据动态地构建一个SQL语句,并根据每个被检查的城市,使用一堆WHERE子句?
我还有其他类型的过滤器,比如类,需要包含在同一个SQL语句中的类型。
如果这没有任何意义,我可以更新这个更好的信息。
更新: Ajax代码
$("input.filter").change(function(){
var data = $("form#refine-associations").serialize();
$.ajax({
type: "post",
url: "<?php echo base_url('association/refine/'.$status) ?>",
cache: false,
data: data,
dataType: "json",
success: function(response){
},
error: function(){
alert('Error while request..');
}
});
});发布于 2016-06-27 23:16:46
我可能会更改City复选框的名称,将数据作为数组传递,并将城市ID存储为值:
<?php echo form_checkbox('cities[]',$i,etc...) ?>HTML结果:
<input type="checkbox" id="city-1" name="cities[]" value=1 class="filter city-filter">
<input type="checkbox" id="city-2" name="cities[]" value=2 class="filter city-filter">
<input type="checkbox" id="city-3" name="cities[]" value=3 class="filter city-filter">然后在你的模型里:
foreach($cities as $city)
{
$this->db->or_where('City', $city);
}很粗糙,从记忆中-但这应该会让你非常接近。
https://stackoverflow.com/questions/38064217
复制相似问题