1

Is it possible to have a javascript code in the controller? I'm trying to have an alert if the login failed,

here is the condition in my controller:

public function login2()
{
    if(!empty($_POST['btn_login'])){
        $session_login = $this->inventory_model->select_login($this->input->post());
        if($session_login){
            $this->session->set_userdata('login_session', 
                array(
                    'Username' => $session_login->Username,
                    'Password' => $session_login->Password,
                    'UserType' => $session_login->UserType
                )
            );
            redirect('inventorysys_controller/homepage');
        }
        else{
            echo "Invalid Username/Password!";
        }
    }
}

I'm trying to put the javascript in the else, so is it possible guys?

Here is my view in case you need a reference:

<form action="login2" method="POST" id="log_form" name="log_form" style="margin-left: auto; margin-right: auto;">
    <div class="modal-body">
       <div class="container">     
          <div id="nav_log1">
            Username: <input type="text" id="txt_user1" name="txt_user1" style="color: black;"/>
            Password: <input type="password" id="txt_pass1" name="txt_pass1" style="color: black;"/>
          </div>
        </div> <!-- /container -->
    <div class="modal-footer" style="padding: 20px; margin-top: 20px;">
        <button type="submit" value="login" id="btn_login" class="btn btn-primary btn-sm" name="btn_login">Sign-In</button>
        <button class="btn btn-warning btn-sm" data-dismiss="modal">Cancel</button>
        <!--<button id="close_me" class="btn btn-default" data-dismiss="modal">Close</button>-->
    </div>
    </div>
 </form>

Your help will be truly appreciated. :)

3
  • echo "<script>alert('Invalid Username/Password!');</script>"; Commented Aug 13, 2014 at 7:29
  • I've tried it, the alert pops up but it is on another blank page not on the login page. Commented Aug 13, 2014 at 7:39
  • It'll be doing it on a blank page because your view which contains your actual page display won't have been loaded before you alert Commented Aug 13, 2014 at 7:43

3 Answers 3

2

Your best and most CodeIgniter/MVC friendly way to do this would be to assign your error message to a variable and then alert it in your view...

public function login2(){
    $data = array();
    if(!empty($_POST['btn_login'])){
        $session_login = $this->inventory_model->select_login($this->input->post());
        if($session_login){
            $this->session->set_userdata('login_session', array('Username' => $session_login->Username,
                                                                'Password' => $session_login->Password,
                                                                'UserType' => $session_login->UserType));
                                                            redirect('inventorysys_controller/homepage');
        }
        else{
            // echo "Invalid Username/Password!";
            $data['error'] = 'Invalid Username/Password!';
        }
    }

    $this->load->view('your-view', $data);
}

In your view:

if (isset($error) && !empty($error)) {
    echo "<script>alert('" . $error . "')</script>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Solved it! Thank you for the help! But the url of my page is changing probably because of the <form action="login2"> in my view but I think I can fix that. Thank you so much! :)
1

Instead of using alerts, if you are using codeigniter try flashdata.

syntax :

To add flashdata:

 $this->session->set_flashdata('item', 'value');

To read a flashdata variable:

 $this->session->flashdata('item');

as per you code :

controller :

public function login2()
{
if(!empty($_POST['btn_login'])){
    $session_login = $this->inventory_model->select_login($this->input->post());
    if($session_login){
        $this->session->set_userdata('login_session', 
            array(
                'Username' => $session_login->Username,
                'Password' => $session_login->Password,
                'UserType' => $session_login->UserType
            )
        );
        redirect('inventorysys_controller/homepage');
    }
    else{
        //echo "Invalid Username/Password!";
         $this->session->set_flashdata('msg', 'Invalid Username/Password!');
        redirect('inventorysys_controller/login_page');
    }
}
}

View:

<form action="login2" method="POST" id="log_form" name="log_form" style="margin-left: auto; margin-right: auto;">
    <div>
            <?php echo $this->session->flashdata('msg');?>
    </div>
    <div class="modal-body">
       <div class="container">     
          <div id="nav_log1">
            Username: <input type="text" id="txt_user1" name="txt_user1" style="color: black;"/>
            Password: <input type="password" id="txt_pass1" name="txt_pass1" style="color: black;"/>
          </div>
        </div> <!-- /container -->
    <div class="modal-footer" style="padding: 20px; margin-top: 20px;">
        <button type="submit" value="login" id="btn_login" class="btn btn-primary btn-sm" name="btn_login">Sign-In</button>
        <button class="btn btn-warning btn-sm" data-dismiss="modal">Cancel</button>
        <!--<button id="close_me" class="btn btn-default" data-dismiss="modal">Close</button>-->
    </div>
    </div>
 </form>

4 Comments

I have a parent construct, should I put the $this->session->flashdata('item','value') in there? And I don't know where to put $this->session->flashdata('item'); Sorry sir, I've tried flashdata before but it didn't work, maybe I did put the codes in the wrong place.
'item' is just name here in my answer i used 'msg'. please look my answer. Alert is not a good idea.
It is not working for me sir, It just redirect on the login page no flash data popping up.
It is working, my bad sir, but it's just text, I don't know how to have buttons on that div, and how can I make it look like a message box.
0

If you want to display javascript from PHP, do the following:

echo "<script>alert('login failed');</script>";

Or more preferrably, build your app in a restful way, that means use ajax requests, to fetch data in probably JSON format from a server (your controller in this case) :)

Hope it helps, in case of any questions, i am here!

Comments

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.