dragon188199
2012-09-13 13:36
浏览 26
已采纳

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

I have a view which has 2 forms:

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

In the controller I check which button was clicked and then I perform some action by adding the corresponding form's values to the database.

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";
}

However when the 'book' button is clicked no action is performed. It is like the button was never clicked. Whereas when I click the 'submit' button, every action is done properly.

In the past I have used the same technique on plain php (i mean no framework, just php), and has worked fine for me. Does codeigniter need any further configuration? Am I doing something wrong?

图片转代码服务由CSDN问答提供 功能建议

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

 &lt; table&gt; 
&lt; th&gt;撰写评论。&lt; / th&gt; 
&lt; tr&gt; 
&lt; td&gt; 
&lt;?php echo form_open($ this-&gt; uri  - &GT; uri_string(),$ form1中);  
 echo form_textarea($ comment); 
 echo form_submit('submit','submit'); 
 echo form_close(); 
?&gt; 
&lt; / td&gt; 
&lt; / tr&gt;  
 
 
&lt; / table&gt; 
 
&lt; table&gt; 
&lt; tr&gt; 
 
&lt; td&gt; 
&lt;?php echo form_open($ this-&gt; uri-&gt; uri_string  ());  
 echo form_dropdown('part',$ part_options);  
 echo form_submit('book','book'); 
 echo form_close(); 
?&gt; 
&lt; / td&gt; 
&lt; / tr&gt; 
&lt; / table&gt; 
   
 
 

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

  if(isset($ _ POST ['book']))
 {
 // //将数据发送到数据库
 echo“单击”书籍“按钮”; \  n} 
 
if(isset($ _ POST ['submit']))
 {
 // //将数据发送到数据库
 echo“提交按钮单击”; 
} 
  <  / pre> 
 
 

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

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dqpfkzu360216 2012-09-13 14:13
    已采纳

    Why not add a hidden field to both forms called form_idwith values 1 and 2 respectively? Easy to catch in your controller upon post; e.g.:

    if($this->input->post()){
      switch($this->input->post('form_id')){
      case 1:
        // do stuff
      break;
      case 2:
        // do stuff
      break;
      }
    }
    
    打赏 评论
  • douji8347 2012-09-13 13:54
    <?php echo form_open($this->uri->uri_string(),$form1); 
    

    and

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

    Looks like you forgot to provide the settings in the second one like:

    <?php echo form_open($this->uri->uri_string(),$form2); 
    
    打赏 评论
  • dongsonghen9931 2012-09-13 21:37

    Well after spending my whole day on this I finaly managed to solve it somehow (although I believe it is not such a propper way to handle this).

    Well:

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

    Probably the forms fields (textarea and dropdown) needed to have the same name (which i have set to 'comment'). Although I do not understand why :/

    Thank you all for trying to help me :)

    打赏 评论

相关推荐 更多相似问题