doumengwei0138
2012-12-27 07:47
浏览 78
已采纳

使用Codeigniter从视图获取数据到控制器

How can I pass the data input by user from view to controller in codeigniter Php using get or post method? I'm currently new to codeigniter..thanks!

AddProduct.php (my view)

<body>
    <form method="POST" action="SaveProductController"></br></br></br>
        <table border='1' align='center'>
            <tr>
                <td>ID: </td><td><input type="text" name="id" 
                                        value="<?php echo $GetProductId->id + 1; ?>" readonly="readonly"></td>
            </tr>
            <tr>
                <td>Description: </td><td><input type="text" name="description"></td>
            </tr>
            <tr>
                <td></td><td><input type="submit" name="addProduct" value="Add Product"><td>
            </tr>
        </table>
    </form>
</body>

SaveProductController .php (my Controller)

class SaveProductController extends CI_Controller{

function index($description){
    $this->load->model('ProductDao');
    $data['id'] = $this->id;
    $data['description'] = $this->description;
    print_r($data);
    //$this->ProductDao->saveProduct();
}

}

ProductDao.php

 function saveProduct() {
    $data = array(
    'id' => $this->input->xss_clean($this->input->post('id')),
    'description' => $this->input->xss_clean($this->input->post('description')),
    'price' => $this->input->xss_clean($this->input->post('price')),
    'size' => $this->input->xss_clean($this->input->post('size')),
    'aisle' => $this->input->xss_clean($this->input->post('aisle')),
    );

    $query = $this->db->insert('mytable', $data);
}

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

如何使用get或post方法将用户从视图输入的数据传递到codeigniter Php中的控制器? 我目前刚接触codeigniter ..谢谢!

AddProduct.php(我的观点)

 &lt; body&gt; 
&lt  ; form method =“POST”action =“SaveProductController”&gt;&lt; / br&gt;&lt; / br&gt;&lt; / br&gt; 
&lt; table border ='1'align ='center'&gt; 
&lt;  tr&gt; 
&lt; td&gt; ID:&lt; / td&gt;&lt; td&gt;&lt; input type =“text”name =“id”
 value =“&lt;?php echo $ GetProductId-&gt; id + 1  ;?&gt;“  readonly =“readonly”&gt;&lt; / td&gt; 
&lt; / tr&gt; 
&lt; tr&gt; 
&lt; td&gt;描述:&lt; / td&gt;&lt; td&gt;&lt; input type =“text”  name =“description”&gt;&lt; / td&gt; 
&lt; / tr&gt; 
&lt; tr&gt; 
&lt; td&gt;&lt; / td&gt;&lt; td&gt;&lt; input type =“submit”name =  “addProduct”value =“添加产品”&gt;&lt; td&gt; 
&lt; / tr&gt; 
&lt; / table&gt; 
&lt; / form&gt; 
&lt; / body&gt; 
   
 
 

SaveProductController .php(我的控制器)

 类SaveProductController扩展CI_Controller {
 
函数索引($ description){
 $ this-  &gt; load-&gt; model('ProductDao'); 
 $ data ['id'] = $ this-&gt; id; 
 $ data ['description'] = $ this-&gt; description; 
 print_r  ($ data); 
 // $ this-&gt; ProductDao-&gt; saveProduct(); 
} 
   
 
 

} \ n

ProductDao.php

  function saveProduct(){
 $ data = array(
'id'=&gt; $ this-&gt; input-&gt;  xss_clean($这 - &GT;输入 - &GT  ; post('id')),
'description'=&gt;  $ this-&gt; input-&gt; xss_clean($ this-&gt; input-&gt; post('description')),
'price'=&gt;  $ this-&gt; input-&gt; xss_clean($ this-&gt; input-&gt; post('price')),
'size'=&gt;  $ this-&gt; input-&gt; xss_clean($ this-&gt; input-&gt; post('size')),
'aisle'=&gt;  $ this-&gt; input-&gt; xss_clean($ this-&gt; input-&gt; post('aisle')),
); 
 
 $ query = $ this-&gt; db-&gt; insert(  'mytable',$ data); 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongpang2029 2012-12-27 13:24
    已采纳

    I have created controller named : Test_controller and view named : manage_test_controller and using below code you can get data from view file to controller.

    Test_controller.php

     class Test_controller extends CI_Controller {
    
            var $controller = "user";
            var $formValues = array();
        function manage_user() {               
    
                    $this->formValues['formAction'] = SITEURL . '/' . 
                    $this->controller . '/manage_' . $this->controller;
    
                    if (isset($_POST['displayName']))
                        $this->formValues['displayName'] = $_POST['displayName']; 
                    else
                        $this->formValues['displayName'] = "";
                    if (isset($_POST['userEmail']))
                        $this->formValues['userEmail'] = $_POST['userEmail']; else
                        $this->formValues['userEmail'] = "";                   
    
                $this->load->view('header');
                $this->load->view($this->controller . '/manage_' . 
                $this->controller, $this->formValues);
                $this->load->view('footer');
            }
        }
    

    Manage_test_controller.php

    <?php echo form_open_multipart($formAction); ?>
    <table>
        <tr>
                <td><?php echo form_label('Display Name'); ?><em>*</em></td>
                <td><?php echo form_input('displayName',$displayName); ?></td>
            </tr>
        <tr>
                <td><?php echo form_label('Email'); ?><em>*</em></td>
                <td><?php echo form_input('userEmail',$userEmail); ?></td>
            </tr>
    </table>
    <?php echo form_close(); ?>
    

    Hope this code will help you.... :)

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongqiao1888 2012-12-27 07:52
    <body>
        <form method="POST" action="<?php echo $this->base_url();?>/controllername/methodname"></br></br></br>
            <table border='1' align='center'>
                <tr>
                    <td>ID: </td><td><input type="text" name="id" 
                                            value="<?php echo $GetProductId->id + 1; ?>" readonly="readonly"></td>
                </tr>
                <tr>
                    <td>Description: </td><td><input type="text" name="description"></td>
                </tr>
                <tr>
                    <td></td><td><input type="submit" name="addProduct" value="Add Product"><td>
                </tr>
            </table>
        </form>
    </body>
    

    observe the change in action

    and in your comtroller specified method get values as shown below

    $id=$this->input->post('id')
    $idescription=$this->input->post('description')
    

    then send these to model and do what ever you want

    load url helper library to make $this->baseurl() to work other wise hard code it using localhost/path...

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题