CodeIgniter - 在视图中形成2个表单

时间:2012-09-13 13:36:59

标签: php forms codeigniter

我有一个视图,它有两种形式:

<table>
<th>Write a comment.</th>
<tr>
    <td>
        <?php echo form_open($this->uri->uri_string(),$form1); 
              echo form_textarea($comment);
              echo form_submit('submit','submit');
              echo form_close();
        ?>
    </td>
</tr> 


</table>

<table>
    <tr>

        <td>
            <?php echo form_open($this->uri->uri_string()); 
                  echo form_dropdown('portion', $portion_options); 
                  echo form_submit('book','book');
                  echo form_close();
            ?>
        </td>
    </tr>
</table>

控制器中,我检查单击了哪个按钮,然后通过将相应表单的值添加到数据库来执行某些操作。

if(isset($_POST['book']))
{
    //sending the data to the database
    echo "Book button clicked";
}

if(isset($_POST['submit']))
{
   //sending the data to the database
   echo "Submit button clicked";
}

但是,单击“书籍”按钮时,不会执行任何操作。这就像按钮从未被点击过。而当我点击“提交”按钮时,每个操作都已正确完成。

过去我在普通的php上使用了相同的技术(我的意思是没有框架,只是php),并且对我来说工作得很好。 codeigniter需要进一步配置吗?我做错了吗?

3 个答案:

答案 0 :(得分:1)

为什么不在两个名为form_id的表单中分别添加隐藏字段,值为1和2?在发布后容易在控制器中捕获; e.g:

if($this->input->post()){
  switch($this->input->post('form_id')){
  case 1:
    // do stuff
  break;
  case 2:
    // do stuff
  break;
  }
}

答案 1 :(得分:0)

<?php echo form_open($this->uri->uri_string(),$form1); 

<?php echo form_open($this->uri->uri_string()); 

您似乎忘了在第二个中提供设置,如:

<?php echo form_open($this->uri->uri_string(),$form2); 

答案 2 :(得分:0)

在花了我一整天的时间之后,我终于设法以某种方式解决了它(尽管我认为它不是这样一种方法来处理这个问题)。

好:

$comment = array(
    'name'      => 'comment',
    'id'        => 'comment',
    'value'     => 'write you comment',
    'row'       => '5',
    'cols'      => '100'
    );

<table>
<th>Write a comment.</th>
<tr>
    <td>
        <?php echo form_open($this->uri->uri_string()); 
              echo form_hidden('form_id', 1);
              echo form_textarea($comment);
              echo form_submit('submit','submit');
              echo form_close();
        ?>
    </td>
</tr> 


</table> 

<table>
    <th>Write a comment.</th>
    <tr>
        <td>

            <?php echo form_open($this->uri->uri_string()); 
                  echo form_hidden('form_id', 2);
                  echo form_dropdown('comment', $portion_options);
                  echo form_submit('book','book');
                  echo form_close();
            ?>

        </td>
    </tr> 


</table> 

表单字段(textarea和dropdown)可能需要具有相同的名称(我已将其设置为“comment”)。虽然我不明白为什么:/

感谢大家帮助我:)