Codeigniter成功或错误

时间:2016-04-14 08:12:25

标签: php codeigniter

public function index()
{
    $this->load->view('home');
    $this->load->model('reg', 'reg');
    $reg = $this->reg->getItems();
    $data['reg'] = $reg;
}

public function error()
{
    $this -> form_validation -> set_rules('username', 'Lietotājvārds', 'is_unique[reg.username]', 'alpha');
    $this -> form_validation -> set_rules('fname', 'Vārds', 'required', 'greater_than[3]');
    $this -> form_validation -> set_rules('lname', 'Uzvārds', 'required', 'alpha_dash', 'valid_email');
    $this -> form_validation -> set_rules('pw', 'Parole' , 'required');
    $this -> form_validation -> set_rules('pw2', 'Atkārtotā Parole' , 'required');
    $this -> form_validation -> set_rules('email', 'Emails' , 'required', 'valid_email');
    $this -> form_validation -> set_rules('email2', 'Atkārtotais Emails' , 'required', 'valid_email');

    $this->form_validation->set_message('required', '{field} 😌 Opā Kāda kļūda.');
    if ($this->form_validation->run())
    {
        $reg['username'] = $this->input->post('username');
        $reg['fname'] = $this->input->post('fname');
        $reg['lname'] = $this->input->post('lname');
        $reg['pw'] = $this->input->post('pw');
        $reg['pw2'] = $this->input->post('pw2');
        $reg['email'] = $this->input->post('email');
        $reg['email2'] = $this->input->post('email2');
        $this->load->model('reg');
        $this -> reg -> saveItem($reg);
    }
    $this->reg_susses();
}

如何制作错误讯息。并且成功的信息。 ??  1.我去注册他在同一页面给我错误我们成功页面。  2.当我成功注册时,他会在第二页给我一个最好的消息。  3.我有1个型号= reg.php  我有1个constroler = home.php  5. 1有2个视图= 1. reg.php 2. = reg_susses.php

1 个答案:

答案 0 :(得分:1)

首先,不要将您的保存方法称为错误,这会产生误导。

保存并传递表单验证后,您可以将用户重定向到成功页面:

public function save()
{
    $this->form_validation->set_rules('username', 'Lietotājvārds', 'is_unique[reg.username]', 'alpha');
    $this->form_validation->set_rules('fname', 'Vārds', 'required', 'greater_than[3]');
    $this->form_validation->set_rules('lname', 'Uzvārds', 'required', 'alpha_dash', 'valid_email');
    $this->form_validation->set_rules('pw', 'Parole', 'required');
    $this->form_validation->set_rules('pw2', 'Atkārtotā Parole', 'required');
    $this->form_validation->set_rules('email', 'Emails', 'required', 'valid_email');
    $this->form_validation->set_rules('email2', 'Atkārtotais Emails', 'required', 'valid_email');

    $this->form_validation->set_message('required', '{field} 😌 Opā Kāda kļūda.');
    if ($this->form_validation->run()) {
        $reg['username'] = $this->input->post('username');
        $reg['fname'] = $this->input->post('fname');
        $reg['lname'] = $this->input->post('lname');
        $reg['pw'] = $this->input->post('pw');
        $reg['pw2'] = $this->input->post('pw2');
        $reg['email'] = $this->input->post('email');
        $reg['email2'] = $this->input->post('email2');
        $this->load->model('reg');
        $this->reg->saveItem($reg);

        // Redirect the user to the success page
        redirect('home/success');
    }

    // If it fails, it will reach this point, so we can load back our view
    $this->load->view('home');
}

// This is the success page
public function success()
{
    $this->load->view('success_page');
}

在您的视图中,您可以显示如下错误消息:

<div>
<?php echo validation_errors() ?>
</div>