C
C
Caspergreen2015-11-08 23:53:47
MySQL
Caspergreen, 2015-11-08 23:53:47

How to send success message after execution?

Good day.
Controller:

public function edit_user(){
        if (!($this->userlib->logged_in())){
            return redirect('admin/users/login');
        }
        $this->data['title'] = "Редактирование пользователя";
        $this->data['css'] = array(
            "assets/css/icons/icomoon/styles.css",
            "assets/css/bootstrap.css",
            "assets/css/core.css",
            "assets/css/components.css",
            "assets/css/colors.css"
        );
        $this->data['js']  = array(
            "assets/js/plugins/loaders/pace.min.js",
            "assets/js/core/libraries/jquery.min.js",
            "assets/js/core/libraries/bootstrap.min.js",
            "assets/js/core/libraries/jquery_ui/core.min.js",
            "assets/js/core/app.js",
            "assets/js/plugins/forms/selects/selectboxit.min.js",
            "assets/js/pages/form_selectbox.js"
        );
        $data = array();
        $url = current_url();
        $user_id = (substr($url, 40, 30));

        $this->form_validation->set_rules('login', 'Имя', 'trim|xss_clean|required');
        $this->form_validation->set_rules('email', 'E-mail', 'trim|valid_email|xss_clean|required');
        $this->form_validation->set_rules('password', 'Пароль', 'trim|xss_clean|min_length[6]|max_length[16]');
        $this->form_validation->set_rules('status', 'Статус', 'trim|xss_clean|required');
 
        if($this->form_validation->run()) {
            $updateData = array(
                'username'    => $this->input->post('login'),
                'email'       => $this->input->post('email'),
            );

            if($this->input->post('password')) {
                $updateData['password'] = sha1(md5($this->input->post('password')));
            }
            if($this->input->post('status') == 'admin'){
                $updateData['status'] = "3";
            } else if($this->input->post('status') == 'moder'){
                $updateData['status'] = "2";
            } else if($this->input->post('status') == 'user'){
                $updateData['status'] = "1";
            } else if($this->input->post('status') == 'banned'){
                $updateData['status'] = "0";
            }

            if($this->user->updateItem($user_id, $updateData)) {
                return redirect('admin/users');
                $this->data['success'] = 'Данные обновлены.';
            }
        }
        $this->data['user']= $this->user->getItem($user_id); 

        $this->load->view('admin/user/edit_user', $this->data);
    }

Data is entered into the database
if($this->user->updateItem($user_id, $updateData)) {
                return redirect('admin/users');
                $this->data['success'] = 'Данные обновлены.';
            }

Required:
After the data is entered, redirect to the admin/users page, and display a success message. Because the view is not loaded, but a redirect is performed. The variable on the admin/users page is not defined and no message is displayed. How to decide?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wellgo, 2015-11-09
@wellgo

/**
 * Redirect with POST data.
 *
 * @param string $url URL.
 * @param array $post_data POST data. Example: array('foo' => 'var', 'id' => 123)
 * @param array $headers Optional. Extra headers to send.
 */
public function redirect_post($url, array $data, array $headers = null) {
    $params = array(
        'http' => array(
            'method' => 'POST',
            'content' => http_build_query($data)
        )
    );
    if (!is_null($headers)) {
        $params['http']['header'] = '';
        foreach ($headers as $k => $v) {
            $params['http']['header'] .= "$k: $v\n";
        }
    }
    $ctx = stream_context_create($params);
    $fp = @fopen($url, 'rb', false, $ctx);
    if ($fp) {
        echo @stream_get_contents($fp);
        die();
    } else {
        // Error
        throw new Exception("Error loading '$url', $php_errormsg");
    }
}

You need a redirect with POST data, pass $this->data and $_POST['success'] will be available in the view.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question