CodeIgniter中的表单POST将不会被发送

时间:2013-05-23 18:54:54

标签: php codeigniter-2

我有一个<select>框,在CodeIgniter的表单助手中生成了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>

当我从下拉列表内容电子商务中选择该选项时,它会以适当的<option>值正确重定向到{{1 }}。但每当我选择第一个选项时,实际上还有一个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); ... }

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; } ,但下拉列表中的第一个选项除外,它显示我echo 1。这有什么不对?

1 个答案:

答案 0 :(得分:3)

if( $this->input->post('selectCatsByComponent') )

应该成为

if( $this->input->post('selectCatsByComponent') !== false )

因为在PHP中0被视为假。

小更新:

Here是PHP考虑false以及您应该使用comparison by type (also known as strict comparison)的所有内容的列表。一篇不错的文章here