1

我有两种形式的视图:

<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 是否需要任何进一步的配置?难道我做错了什么?

4

3 回答 3

1

form_id为什么不向分别以值 1 和 2调用的两个表单添加一个隐藏字段?发布后很容易抓住你的控制器;例如:

if($this->input->post()){
  switch($this->input->post('form_id')){
  case 1:
    // do stuff
  break;
  case 2:
    // do stuff
  break;
  }
}
于 2012-09-13T14:13:36.310 回答
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); 
于 2012-09-13T13:54:17.753 回答
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> 

可能表单字段(文本区域和下拉菜单)需要具有相同的名称(我已将其设置为“评论”)。虽然我不明白为什么:/

谢谢大家试图帮助我:)

于 2012-09-13T21:37:30.697 回答