【问题标题】:CodeIgniter - 2 forms in a viewCodeIgniter - 一个视图中的 2 个表单
【发布时间】:2012-09-06 14:50:10
【问题描述】:

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

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

【问题讨论】:

  • 哦,告诉我 $form1 中的数组。它在哪里?
  • 您应该可以在一个页面上拥有 2 个表单。您可以发布您在浏览器中点击的 uri_string,然后发布您的整个方法吗?
  • 你能把表格生成的 html 贴出来吗?
  • 我确实有它在我的视图顶部。这是数组: $form1 = array( 'name' => 'form1', 'id' => 'form1' );
  • 抱歉耽搁了。我正在查看问题是否在下拉列表中,也许它在那里,因为我将输入从下拉列表更改为文本输入,现在点击的按钮正在被识别

标签: php forms codeigniter


【解决方案1】:
<?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); 

【讨论】:

  • 否,使用 uri->uri_string());或 uri->uri_string(),$form2);不起作用:/ $form1 = array('name' => 'form1', 'id' => 'form1');和 $form2 = array( 'name' => 'form2', 'id' => 'form2' );
  • 当我将表单合并为 1 时,会识别单击了哪个按钮并正常工作。但是,如果可能的话,我需要它们使用单​​独的表格。
  • 在任何情况下都不要在 CodeIgniter 中使用if(isset($_POST['book']))。使用内置$this-&gt;input-&gt;post('book');
  • 并在 firebug/chrome 开发者工具中查看正在发送到控制器的数据,可以在 Network 选项卡中查看。
  • 尽管我不知道如何使用 firebug,但在“网络”选项卡的“标题”选项卡中,我看到我从下拉列表中选择的内容已发送
【解决方案2】:

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

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

【讨论】:

  • 感谢您的回复。我只是按照您的方式尝试过,但是仍然只能识别“提交”按钮
  • 您使用的是if($this-&gt;input-&gt;post('book')) {还是原始if(isset($_POST['book'])) {?
  • 实际上我使用了 $this->input->post() ,因为我认为从 swich 中可以理解发布的表单。我现在尝试使用 $this->input->post('book') 仍然没有。
  • 我刚刚在本地测试过,echo form_hidden('form_id', 1); 和 echo form_hidden('form_id', 2); 分别在两个表单上。在控制器中我做if($this-&gt;input-&gt;post()){ echo $this-&gt;input-&gt;post('form_id'); } 我得到正确的输出 - 所以它应该工作。
  • 我想我明白了。您正在检查 if(isset($_POST['submit'])) 因为两个按钮都是 submit 按钮,所以您的第二个 if 将始终评估为 true。因此,您需要检查的不是submit 的存在,而是另一个元素的存在,即我建议的隐藏字段。或者,您可以这样做:if($_POST['submit'] == 'submit'){ 和 if($_POST['submit'] == 'book'){
【解决方案3】:

好吧,在花了我一整天的时间之后,我终于设法以某种方式解决了它(尽管我相信这不是处理这个问题的正确方法)。

嗯:

$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> 

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

感谢大家帮助我:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 2020-02-28
    相关资源
    最近更新 更多