GGsThePlayDude
GGsThePlayDude

Reputation: 57

keeping the inputted value at is if form is submitted with codeigniter

This is my login form:

     <div class="form-container">
        <h1 style="text-align:left">Login Here!</h1><br>
        <form class="form-horizontal" action="<?php echo base_url(); ?>index.php/User_Authentication_Controller/login_user" method="POST">
          <div class="error_msg1"></div>
          <input autofocus="autofocus" type="text" name="txt_login_username" class="form-control" placeholder="Username.."/><div class="error_msg2"></div>
          <input type="password" name="txt_login_password" class="form-control" placeholder="Password.." /><div class="error_msg3"></div>
          <center><button class="btn btn-info" type="submit">Login</button><br/><br/>
          <a href="<?php echo base_url();?>index.php/Pages_Controller/show_forgotpassword">Forgot Password?</a></center>
        </form>
    </div>

This is my controller:

public function login_user(){
    $username = $this->input->post('txt_login_username');
    $password = md5($this->input->post('txt_login_password'));
    $result=$this->LogIn_Model->login($username,$password);

    if (!empty($username) && !empty($password)) {
    if($result!=null){
        $_SESSION["username"] = $username;
        $_SESSION["id"] = $result["0"]["Account_no"];
        $_SESSION["Usertype"] = $result["0"]["Usertype"];
        if($result["0"]["Status"] == 1){
            if($result["0"]["Usertype"] == 0){
                redirect('User_Authentication_Controller');
            }
            else if($result["0"]["Usertype"] == 1){
                redirect('Pages_Controller/show_adminpage/');           
            }
        }
        else if($result["0"]["Status"] == 0){
            redirect('User_Authentication_Controller');             
        }
    }
    else{
        redirect('User_Authentication_Controller');
    }
   }
   else{
        redirect('User_Authentication_Controller'); 
   } 

}

This actually works!

Now my question is:

As you see in the else if($result["0"]["Status"] == 0){}, all I want is that after I submitted the page it will redirect back to the login, and I want that the

   <div class="error_msg1"></div>

will show an error telling, "Your account has been deactivated by the Admin" And if he inputs a username it will stay still as he submit it! how will I do that? If he inputs Username: Michael and Inputs a wrong password, I want the Username: Michael to stay still! How will I do that after the form submit?

Upvotes: 4

Views: 68

Answers (2)

ch271828n
ch271828n

Reputation: 17597

Way 1

Use AJAX instead of form. Your js make an ajax post to your server, and then get the result. Depending on it, your js reacts, for example, showing error messages. This is a good choice.

Way 2

For example, you can set $_SESSION['ShowErrorMsg1']=true in login_user, then in your login page php, just say:

.

if(isset($_SESSION['ShowErrorMsg1']) && $_SESSION['ShowErrorMsg1']==true){
    echo '<div class="error_msg1"></div>';
}

then the error msg will be shown.

UPDATE:

The main idea is that you can storage anything you want to pass it from one php to another php in $_SESSION. If you want to pass on $msg_content for example, just use $_SESSION['somekey']=$msg_content; in one php. Then in another php use $_SESSION['somekey'] who storages the data of $msg_content.

Upvotes: 1

Vikas
Vikas

Reputation: 121

you can use set_userdata to store error msg and can show: in your controller:

else if($result["0"]["Status"] == 0){
$this->session->set_flashdata('error_msg1','Some Error Msg!'); 
}

and in your view:

<?php if($this->session->flashdata('error_msg1')) : ?>
<div class="alert alert-success">
    <span class="alert-rr">
        <i class="fa fa-check-circle" aria-hidden="true"></i>
    </span>
    <span class="alert-msg">
        <?php echo $this->session->flashdata('error_msg1'); ?>
    </span>
</div>
<?php endif; ?>

Upvotes: 2

Related Questions