我有一个<select>
盒子,在CodeIgniter的Form Helper中生成了3个<option>
项目
<select onchange="this.form.submit()" name="selectCatsByComponent">
<option value="0" selected="selected">-- Choose a component --</option>
<option value="1">Content</option>
<option value="2">E-commerce</option>
</select>
当我从下拉列表Content或 ecommerce 中选择选项时,它将以适当的<option>
值正确重定向到categories/get_categories/$ID
页面。但是每当我选择第一个选项时,它实际上也有一个value="0"
,它将我重定向到没有ID的categories/get_categories/
,而不是重定向到categories/get_categories/0
…我就是不能解决这个问题。
这是我的控制器Categories.php
:
public function get_categories($pid = 0, $com_id = 0){
...
// Check if Filter sends some POST data
if( $this->input->post('selectCatsByComponent') ){
echo '1';
$com_id = $this->input->post('selectCatsByComponent');
$this->session->set_userdata(array('selectCatsByComponent' => $com_id));
}else
echo '2';
$this->session->set_userdata(array('selectCatsByComponent' => $com_id));
// Get processed data results
$componentData['selectedComponent'] = $com_id;
$componentData['selectComponents'] = $this->component_model->get_components();
$componentData['items'] = $this->category_model->getCategoryList($pid, $com_id);
...
}
And categories_model.php
:
public function get_categories($parent = FALSE, $com_id = FALSE){
// SQL command
$this->db->select('id, com_id');
$this->db->order_by('categories.ordering', 'ASC');
// Check if parent ID exist
if( $parent !== FALSE ) $this->db->where('pid', $parent);
// Check if component ID exist
if( $com_id != FALSE ) $this->db->where('com_id', $com_id);
// Alphabetize results if no ordering present
$this->db->order_by('title', 'ASC');
$query = $this->db->get('categories');
// Check row existance in DB table
if( $query->result() < 1 ) return FALSE;
// Get results in object type
$result = $query->result();
$categories = array();
$components = array();
foreach($result as $cat){
$categories[] = $this->get_category($cat->id);
$components[] = $this->get_component($cat->com_id);
}
return $categories;
}
请问,谁能告诉我我做错了什么?为什么它不POST表单?在控制器的函数中,我做了一个条件语句,在这里我回显出数字。每当我选择选项时,它都会显示我echo 1
,除了下拉列表中的第一个选项,它显示我echo 2
。
if( $this->input->post('selectCatsByComponent') )
变成
if( $this->input->post('selectCatsByComponent') !== false )
因为0在PHP中被认为是false。
小更新:这里列出了PHP认为false
的所有内容,以及您应该使用按类型比较(也称为严格比较)的地方。