【问题标题】:keeping the inputted value at is if form is submitted with codeigniter如果使用 codeigniter 提交表单,则将输入值保持在 is
【发布时间】:2017-02-10 11:34:17
【问题描述】:

这是我的登录表单:

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

这是我的控制器:

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'); 
   } 

}

这确实有效!

现在我的问题是:

正如您在else if($result["0"]["Status"] == 0){} 中看到的,我想要的只是在我提交页面后它会重定向回登录,我希望

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

会显示一个错误提示,“您的帐户已被管理员停用”如果他输入用户名,它会在他提交时保持静止!我将如何做到这一点?如果他输入用户名:Michael 并输入错误的密码,我希望用户名:Michael 保持不动!表单提交后我将如何处理?

【问题讨论】:

    标签: php html codeigniter


    【解决方案1】:

    方式 1

    使用AJAX 代替form。您的 js 将 ajax 发布到您的服务器,然后得到结果。取决于它,您的 js 会做出反应,例如,显示错误消息。这是一个不错的选择。

    方式 2

    比如你可以在login_user中设置$_SESSION['ShowErrorMsg1']=true,然后在你的登录页面php中,就说:

    .

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

    然后会显示错误消息。

    更新:

    主要的想法是你可以存储任何东西你想将它从一个php传递到$_SESSION中的另一个php。例如,如果您想传递$msg_content,只需在一个php 中使用$_SESSION['somekey']=$msg_content;。然后在另一个 php 中使用$_SESSION['somekey'] 存储$msg_content 的数据。

    【讨论】:

    • 我喜欢另一种方式,因为我还不知道如何使用 AJax!但我的问题是我将如何获得error_msg1? bcoz 它在课堂上,如果我将类更改为 name,我仍然无法获得值 bcoz 类型不是输入它是 div!我将如何把它放在 login_user 中?
    • 谢谢伙计!现在我真的明白了! $_SESSION 是个很有用的人!非常感谢!
    【解决方案2】:

    您可以使用 set_userdata 来存储错误消息,并可以显示: 在您的控制器中:

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

    在你看来:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-25
      • 1970-01-01
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 2013-10-16
      相关资源
      最近更新 更多