MVC:从视图到控制器的输入中检索值

时间:2015-11-24 09:41:04

标签: php codeigniter mysqli

我知道这似乎是一个重复的问题。但我已创建此表单,我想获取电子邮件字段的值,以便生成它到二维码 所以这是视图

<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4>Registration form</h4>
            </div>
            <div class="panel-body">
                <?php $attributes = array("name" => "registrationform");
                echo form_open("user/register", $attributes);?>
                <div class="form-group">
                    <label for="name">First Name</label>
                    <input class="form-control" id="fname" name="fname" placeholder="Your First Name" type="text" value="<?php echo set_value('fname'); ?>" />
                    <span class="text-danger"><?php echo form_error('fname'); ?></span>
                </div>

                <div class="form-group">
                    <label for="name">Last Name</label>
                    <input class="form-control" name="lname" placeholder="Last Name" type="text" value="<?php echo set_value('lname'); ?>" />
                    <span class="text-danger"><?php echo form_error('lname'); ?></span>
                </div>

                <div class="form-group">
                    <label for="email">Email ID</label>
                    <input class="form-control" id="email" name="email" placeholder="Email-ID" type="text" value="<?php echo set_value('email'); ?>" />
                    <span class="text-danger"><?php echo form_error('email'); ?></span>
                </div>

                <div class="form-group">
                    <label for="subject">Password</label>
                    <input class="form-control" name="password" placeholder="Password" type="password" />
                    <span class="text-danger"><?php echo form_error('password'); ?></span>
                </div>

                <div class="form-group">
                    <label for="subject">Confirm Password</label>
                    <input class="form-control" name="cpassword" placeholder="Confirm Password" type="password" />
                    <span class="text-danger"><?php echo form_error('cpassword'); ?></span>
                </div>

                <div class="form-group">
                    <button name="submit" type="submit" class="btn btn-default">Signup</button>
                    <button name="cancel" type="reset" class="btn btn-default">Cancel</button>
                </div>
                <?php echo form_close(); ?>
                <?php echo $this->session->flashdata('msg'); ?>
          </div>
          </div>
         </div>

控制器: 这是一个将数据插入数据库并重定向到另一个函数(successInsert)

的函数
function register()
    {
        //set validation rules
        $this->form_validation->set_rules('fname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]');
        $this->form_validation->set_rules('lname', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[30]');
        $this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email|is_unique[user.email]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[cpassword]|md5');
        $this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|md5');

        //validate form input
        if ($this->form_validation->run() == FALSE)
        {
            // fails
            $this->load->view('user_registration_view');
        }
        else
        {
            //insert the user registration details into database
            $data = array(
                'fname' => $this->input->post('fname'),
                'lname' => $this->input->post('lname'),
                'email' => $this->input->post('email'),
                'password' => $this->input->post('password')
            );

            // insert form data into database
            if ($this->User_model->insertUser($data))
            {
                redirect('user/successInsert');
         }
}



    function successInsert()
            {
                $data['email'] = $this->input->post("email");  
                $this->load->view('successful',$data);
            }

//此功能仅使用电子邮件重定向到名为“成功”的视图 然而,我仍然无法回复电子邮件。请帮忙。

2 个答案:

答案 0 :(得分:1)

您正在将用户重定向到另一个不保持$this->load->view('successful',$data);数据完整的页面。我建议只加载视图User_model->insertUser(),而不是重定向,从而将所有内容保存在一个函数中。

注意:我不确定justify-content: center函数中发生了什么,但请确保在将原始数据插入任何数据库之前将其转义。

答案 1 :(得分:-1)

更改表单操作

HibernateConstraintValidatorContext

要:

  <?php $attributes = array("name" => "registrationform");
            echo form_open("user/register", $attributes);?>