简体   繁体   中英

How to call a javascript function in controller of codeigniter?

i have a view like this

<p>
   <div id="error_msg" class="alert alert-error"> //at this div have css .alert{display:none;}
      <a class="close click_close">X</a>
      <strong>oops!</strong> username atau password yang anda masukan salah
   </div>
</p>

and in my controller

public function login_form() {
    $this->form_validation->set_rules('username', 'Username', 'required|trim|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'required|md5|xss_clean');
    $this->form_validation->set_error_delimiters('<span class="error">', '</span>');

    if($this->form_validation->run()==FALSE) {
        $this->load->view('v_admin_login');
    } else {
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $cek = $this->m_admin_login->getAdmin($username, $password, 1, 1);

        if($cek <> 0) {
            $this->session->set_userdata('isLogin', TRUE);
            $this->session->set_userdata('username',$username);
            $this->session->set_userdata('level',$level);

            redirect('admin');
        } else {
            //I want to change class alert to .alert{display:block}
            //What should I do?
        }
    }
}

the point is i want to show div with class alert if input username or password was wrong..

Print the javascript code as if a string with ECHO command, but don't forget to put the tag . See my example:

<?

class Test extends CI_Controller {

    function Test() {
        parent::__construct();

    }

    function index() {

        echo "<script language=\"javascript\">alert('test');</script>";
    }
}

Their is not possible to include javascript code into controller.

Possible Solutions

Method one:

Create one error view and when ever you want to show some error redirect user to that page.

Method two:

public function login_form() {


    $CI = & get_instance(); /// Added New line


    $this->form_validation->set_rules('username', 'Username', 'required|trim|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'required|md5|xss_clean');
    $this->form_validation->set_error_delimiters('<span class="error">', '</span>');

    if($this->form_validation->run()==FALSE) {
        $this->load->view('v_admin_login');
    } else {
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $cek = $this->m_admin_login->getAdmin($username, $password, 1, 1);

        if($cek <> 0) {
            $this->session->set_userdata('isLogin', TRUE);
            $this->session->set_userdata('username',$username);
            $this->session->set_userdata('level',$level);

            redirect('admin');
        } else {
      /// Added New Section
           $this->theme->set_message('Your error Msg', 'error');
           $data = array("username" => $this->input->post('username');,
                "password" => $this->input->post('password');,
                "ci" => $CI
            );
        }
    }

    $this->theme->view($data, "Your view page"); /// Added New line

}

Now in View

Put this line where you want to show msg

<?php echo $ci->theme->message(); ?>   /// Added New line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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