3

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

1
  • 1
    You have to do validation in ajax. Then you can easily show error message in alert box. Commented Dec 20, 2013 at 10:09

2 Answers 2

6

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>";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

thankyou, but i want to show my error msg at my view with my error message like at div with class alert i just want to show this one if user name or password was wrong
$this->theme->set_message('Your error Msg', 'error'); replace your error message with first parameter. And put <?php echo $ci->theme->message(); ?>\ this in your view part where you want to display error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.