最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to call a javascript function in controller of codeigniter? - Stack Overflow

programmeradmin4浏览0评论

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

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

Share Improve this question asked Dec 20, 2013 at 9:48 madrickmadrick 2211 gold badge3 silver badges16 bronze badges 1
  • 1 You have to do validation in ajax. Then you can easily show error message in alert box. – Kumar V Commented Dec 20, 2013 at 10:09
Add a ment  | 

2 Answers 2

Reset to default 6

Print the javascript code as if a string with ECHO mand, 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
发布评论

评论列表(0)

  1. 暂无评论