如何通过form_open_multipart将id从视图发送到Controller CodeIgniter

时间:2015-12-27 13:54:33

标签: php html codeigniter codeigniter-2 codeigniter-3

我正在尝试从视图向CodeIgniter中的控制器发送id。我的要求是根据按钮id切换功能。这是我的HTML代码。

查看HTML

 <?php echo form_open_multipart('upload_control/switch_load','id="bt_addImage"');?>
 <input id= "bt_addImage" type="submit" value="Add Image" /> <br>
 <?php echo form_open_multipart('upload_control/switch_load','id="bt_chooseImage"');?>
 <input type="submit" id="bt_chooseImage" value="Submit"/><br>

Upload_control.php代码

public function switch_load($id)
{
    if($id == "bt_addImage")
    {
        do_loadcategories();
    }
    else
    {
        do_upload();
    }
}
public function do_loadcategories()
{
    //code list categories
}
public function do_upload()
{
    //code to upload
}

是不是很正确?

还有其他办法吗?

帮我解决问题。

2 个答案:

答案 0 :(得分:1)

在视图中

$hiddenFields = array('id' => 'bt_addImage'); # add Hidden parameters like this
echo form_open_multipart('upload_control/switch_load', '', $hiddenFields);

在控制器中

public function switch_load()
{
    $id = $this->input->post('id');
    if($id == "bt_addImage")
    {
        do_loadcategories();
    }
    else
    {
        do_upload();
    }
}

查看看起来像这样

<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/upload_control/switch_load">
<input type="hidden" name="id" value="bt_addImage" /> # hidden filed.

Codeigniter Form Helper

答案 1 :(得分:0)

改变
if($id == "bt_addImage")

if($this->input->post('id') == "bt_addImage")