如何在codeigniter的控制器中调用javascript函数?

时间:2013-12-20 09:48:35

标签: javascript php codeigniter

我有这样的观点

<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>

并在我的控制器中

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?
        }
    }
}

关键是我想在输入用户名或密码错误的情况下用class alert显示div。

2 个答案:

答案 0 :(得分:6)

打印javascript代码,就像使用ECHO命令的字符串一样,但不要忘记放置标记。看我的例子:

<?

class Test extends CI_Controller {

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

    }

    function index() {

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

答案 1 :(得分:2)

他们无法将javascript代码包含在控制器中。

可能的解决方案

方法一:

创建一个错误视图,当您想要显示某个错误时,将用户重定向到该页面。

方法二:

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

}

现在查看

将此行放在要显示msg的位置

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