php – 保持输入的值是表单是否与codeigniter一起提交
这是我的登录表单:
<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中看到的那样($result [“0”] [“Status”] == 0){},我想要的是,在我提交页面后,它会重定向回登录,我希望 <div class="error_msg1"></div> 将显示一个错误,告诉您“您的帐户已被管理员停用”并且如果他输入用户名,它将在提交时保持不变!我该怎么做?如果他输入用户名:Michael并输入错误的密码,我希望用户名:Michael保持不动!表格提交后我将如何做到这一点? 解决方法
方式1
使用AJAX而不是表单.你的js向你的服务器发一个ajax帖子,然后得到结果.根据它,您的js会做出反应,例如显示错误消息.这是一个不错的选择. 方式2 例如,您可以在login_user中设置$_SESSION [‘ShowErrorMsg1’] = true,然后在您的登录页面php中,只需说: . if(isset($_SESSION['ShowErrorMsg1']) && $_SESSION['ShowErrorMsg1']==true){ echo '<div class="error_msg1"></div>'; } 然后将显示错误消息. 更新: 主要的想法是你可以在$_SESSION中存储任何想要将它从一个php传递到另一个php的东西.例如,如果你想传递$msg_content,只需使用$_SESSION [‘somekey’] = $msg_content;在一个PHP.然后在另一个php中使用$_SESSION [‘somekey’]存储$msg_content的数据. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |