显示html选项值codeigniter

byqmnocz  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(110)

I'm new in Codeignite. I have a "Test" Controller with "index()" and "view($id)" functions. the index method goes to "test1.php" view. In "test1.php", I have a dropdown options. if I submit on this page, the "view" method of "Test" Controller will be called. My question is how can I pass the option value to "view" function , so that the argument of method will be set to the option value and then the url would be something like "http://localhost/test/view/id" which the id is option value from "test1.php"
Test Controller

class Test extends CI_Controller{
   public function indx() {
     //some code
      $this->load->view('test1.php');
   }
   public function view($id)
   {
     //some code, here I  use $id which I want to be option value from test1.php
   }

test1.php

<?php 
$options = array(
              '1'  => 'One',
              '2'    => 'Two',
              '3' => 'Three',
              '4' => 'Four',
            );

$js = 'id="shirts" onChange="this.form.submit();"';
echo form_dropdown('shirts', $options, '1', $js);
/* here I want to call echo form_open() as echo form_open("/test/view/[option  value]") but I don't know how to do this;*/
c0vxltue

c0vxltue1#

您可以加载包含数据视图

class Test extends CI_Controller{
   public function index($id) {
      $this->data['result'] = get_results($id);
      $this->load->view('test1.php', $this->data);
   }
}

在视图文件中:

foreach ($results as $result) {
   // action
}
ws51t4hk

ws51t4hk2#

如果必须在URL中传递ID,那么您需要使用javascript,在select字段上使用change事件侦听器将ID附加到表单操作URL。
然而,IMHO,您最好只从POST数据中检索view()方法内的值。
使用
$id = $this-〉input-〉post('shirts',TRUE)
,而不是通过URL强制它。

相关问题