简体   繁体   English

如何将值从视图页面传递到控制器

[英]How to pass the value from view page to controller

I am calling the controller function via ahref tag and i want to pass the current record no to the controller to update the record current value is stored in$currentID and i pass this value in input type..but i didn't get the value from the view page..i tried var_dump($id)..it shows null Controller Code to update: 我正在通过ahref标记调用控制器功能,我想将当前记录no传递给控制器​​以更新记录当前值存储在$ currentID中,并且我将该值传递给输入类型..但是我没有得到该值从视图页面..我尝试了var_dump($ id)..它显示了要更新的空控制器代码:

    public function update($id='')
{

      $session_data = $this->session->userdata('logged_in');
        $data['username'] = $session_data['username'];

        $id=$this->input->post('update');
        echo "<pre>";var_dump($id);
            $dc=$this->input->post('dc');
            if($dc=='c'){
            $amount=$this->input->post('credit1');
                        } 
            else if ($dc=='d') {
            $amount=$this->input->post('debit');
                                }

$data=array(
      'date' =>$this->input->post('TDate'),
      'code' =>$this->input->post('TName'),
      'project' =>$this->input->post('TName1'),
      'part' =>$this->input->post('part1'),
      'part1' =>$this->input->post('part2'),
      'dc'=>$this->input->post('dc'),
      'amount'=>$amount,
   );
$this->db->where('recno', $id);
$this->db->update('daybook', $data);
$this->session->set_flashdata('Add1', 'Updated Successfully'); 
redirect('BookKeeping/daybook','refresh');
}

calling update 呼叫更新

<a href="<?=site_url('BookKeeping/update/'.$currentID)?>" class="btn btn-info btn-"><i class="icon-new position-left">
                        <input type="hidden" name="update" id="id" value="<?php echo $currentID?>">
                    </i>Update</a>

Help me to pass the currentvalue to the controller..Thanks in advance 帮助我将currentvalue传递给控制器​​。

you are sending value in url change this 您正在发送网址中的值更改此

$id=$this->input->post('update');

To

$id=$this->uri->segment('3');

remove the input field. 删除输入字段。 You can't pass any input data with 您不能通过传递任何输入数据

<a href="<?=site_url('BookKeeping/update/'.$currentID)?>" class="btn btn-info btn-"><i class="icon-new position-left"></i>Update</a>

Try this 尝试这个

public function update($id=''){

$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];

$id=$id;

echo "<pre>";var_dump($id);
$dc=$this->input->post('dc');
if($dc=='c'){
  $amount=$this->input->post('credit1');
} 
else if ($dc=='d') {
  $amount=$this->input->post('debit');
}

$data=array(
  'date' =>$this->input->post('TDate'),
  'code' =>$this->input->post('TName'),
  'project' =>$this->input->post('TName1'),
  'part' =>$this->input->post('part1'),
  'part1' =>$this->input->post('part2'),
  'dc'=>$this->input->post('dc'),
  'amount'=>$amount,
);

$this->db->where('recno', $id);
$this->db->update('daybook', $data);
$this->session->set_flashdata('Add1', 'Updated Successfully'); 
redirect('BookKeeping/daybook','refresh');

} }

In your code 在你的代码中

As I see your controller code posted in question, you haven't passed any value in variable $currentID which might be getting undefined, so please first try to var_dump your $currentID variable and check its value. 当我看到有问题的控制器代码发布时,您尚未在变量$currentID传递任何值,该值可能未定义,因此请首先尝试var_dump您的$currentID变量并检查其值。

Do a test 做个测试

Please try assigning a static id for the sake of functionality. 为了功能起见,请尝试分配静态ID。 Your static value will be passed to the method as an argument. 您的静态值将作为参数传递给方法。 Once you sure about static value is working, you can go ahead with finding out where is the issue in $currentID variable. 一旦确定静态值有效,就可以继续找出$currentID变量中的问题所在。

$data['currentID'] = $someValueFromDB;
$this->load->view('some_view.php',$data);

this way your value will be passed to view, make sure value is correctly passing or getting generated on view. 这样,您的值将被传递到视图,确保值在视图上正确传递或生成。 You are passing data in right way at controller, no problem with that. 您正在控制器上以正确的方式传递数据,这没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM