使用form_open时,从控制器传递变量到视图

时间:2015-02-23 16:16:51

标签: php codeigniter-2

我对codeigniter很新,我想知道是否有人可以帮助我。我想将一个变量从我的控制器传递给我的视图,但是我这样做它总是给出一个未定义的变量错误。另外,如果我保持load->视图调用也会出错! 那么,我怎样才能将变量传递给视图?在此先感谢您的帮助!

<?php <!-- view -->
    echo form_open('match/setDefafultEmail', array(
        'class' => '',
    ));
?>
<div class="well">
<div class="text-center">
    <h4>Set Default Email</h4>
</div>
<?php 

echo("<div>");
echo form_label('Email:');
echo form_input(array(
                    'id' => 'email_address',
                    'name' => 'email_address',
                    'type' => 'email',
                    'placeholder' => 'email@example.com',
                    'value' => set_value('email_address'),
                    'required' => '',
                    'title' => 'Email address'
                    ));
echo("</div>");

echo("<div>");
echo form_submit(array(
    'id' => 'btn',
    'name' => 'min',
    'type' => 'Submit',
    'class' => 'btn btn-info',
    'value' => 'Set Default Email'
));
echo("</div>");
    echo form_close()
?>
<br>
<br>
<table class="table" >
    <thead>
        <tr>
            <th>Name</th>
            <th>Default Email</th>
        </tr>
    </thead>
    <tr class="info">
        <!--display current default name and email--> 
       <?php foreach($info as $row):?>
        <td> <?php echo $row->full_name ?> </td>
        <td> <?php echo $row->email ?> </td>
        <?php endforeach;?>
    </tr>
</table>        

    public function setDefafultEmail(){

    $this->load->library('form_validation');
    $this->form_validation->set_rules('email_address', 'Email Address', 'valid_email');

    if ($this->form_validation->run( ) == true){

        $name = $this->input->post('full_name');
        $default_email = $this->input->post('email_address');
        $this->spw_vm_request_model->setEmailToDefault($name,$default_email);
    }
    $data['info'] = $this->request_model->getDefaultEmailAndName();
    $this->load->view('admin/admin_dashboard',$data);
    redirect('admin/admin_dashboard');
}

1 个答案:

答案 0 :(得分:0)

1)加载视图后,您不需要重定向。当您重定向('admin / admin_dashboard')时,您实际上调用了admin_dashboard控制器 2)在您的视图上,您​​尝试将数组作为对象访问( $ row-&gt; full_name )我认为您应该使用$ row ['full_name']。
3)如果你的Model返回一个对象,你必须在你的Controller中序列化它并在你的View中反序列化它。