在按钮上显示Flash消息使用PHP单击Codeigniter

时间:2016-11-14 05:32:57

标签: php codeigniter message buttonclick flash-message

我创建了一个控制器方法,允许用户更改他/她的登录凭据(用户名,密码)。单击保存更改按钮,我想显示一条指示更改过程状态的Flash消息。

但是我遇到了一个小问题,只有当我刷新网址时才显示flash消息,而不是在点击保存更改按钮后立即显示。

查看

<div id="login">
     <section class="clearfix">
     <form class="form-horizontal" method = "post" action="<?php echo site_url('studentDashboardController/saveUserLoginDetails');?>">
     <fieldset>

     <div style="width:1363px;margin-left:10px">
         <?php echo $this->session->flashdata('msgLogin'); ?>
     </div>

     <!-- Form Name -->
          <legend>Edit Login Credentials</legend>

          <!-- Text input-->
          <div class="form-group">
               <label class="col-md-4 control-label" for="username">Username</label>
               <div class="col-md-6">
                   <input id="username" name="username" type="text" placeholder="current" class="form-control input-md">
                   <span class="help-block">Enter a new Username (Leave Blank to only change the Password)</span>
               </div>
          </div>

          <!-- Password input-->
          <div class="form-group">
               <label class="col-md-4 control-label" for="password">Password</label>
               <div class="col-md-6">
                    <input id="password" name="password" type="password" placeholder="New password" class="form-control input-md">

               </div>
          </div>

          <!-- Password input-->
          <div class="form-group">
               <label class="col-md-4 control-label" for="confirmPassword">Confirm Password</label>
               <div class="col-md-6">
                    <input id="confirmPassword" name="confirmPassword" type="password" placeholder="Retype password" class="form-control input-md" required="">
               </div>
          </div>

          <!-- Button -->
               <div class="form-group">
                   <label class="col-md-4 control-label" for=""></label>
                   <div class="col-md-4">
                        <a href="<?php echo base_url() ?>index.php/studentDashboardController/saveUserLoginDetails">
                             <button id="submit" name="submit" type="submit" class="btn btn-primary">Save Changes</button>
                        </a>
                   </div>
               </div>

           </fieldset>
       </form>

    </section>
</div>

studentDashboardController - saveUserLoginDetails()

public function  saveUserLoginDetails()
{
     ...
     $msg = $this->studentprofileModel->saveUserLoginDetails($user,$this->input->post());
     $msgText = $msg['msg'];
     $msgType = $msg['type'];
     //Loading View
     $data1['details'] = $this->studentprofileModel->getUserDetails($username);
     if($msgType == "danger")
     {
         $this->session->set_flashdata('msgLogin', '<div class="alert alert-danger text-center">Failed</div>');
         echo '<script type="text/javascript">alert("'.$msgText.'")</script>';
     }
     else
     {
         $this->session->set_flashdata('msgLogin', '<div class="alert alert-success text-center">Success</div>');
     }

     redirect(base_url('index.php/studentDashboardController/editProfile',$data1));
}

studentprofileModel - saveUserLoginDetails($ user,$ this-&gt; input-&gt; post())

public function saveUserLoginDetails($uid,$post)
{
    $username = $post['username'];
    $password = $post['password'];
    $confirmPassword = $post['confirmPassword'];

    if ($username == null || $username == "") {
        if ($password == $confirmPassword) {
            $data = array(
                'password' => $password,
            );
            $this->db->where("uid", $uid);
            $this->db->update("sysuser", $data);
            $msg = "Password has been successfully changed";
            $type = "success";
            $msgData = array(
                'msg' => $msg,
                'type' => $type
            );
            return $msgData;
        }
    }
    else
    {
        if($password == $confirmPassword)
        {
            $data = array(
                'password' => $password,
                'username' => $username
            );

            $this->db->where("uid", $uid);
            $this->db->update("sysuser", $data);
            $this->load->library('session');
            $sess_array = array(
                'username' => ''
            );
            $this->session->unset_userdata('logged_in', $sess_array);
            $session_data = array(
                'username' => $username
            );
            // Add user data in session
            $this->session->set_userdata('logged_in', $session_data);

            $msg = "Login Data have been successfully updated";
            $type = "success";
            $msgData = array(
                'msg' => $msg,
                'type' => $type
            );
            return $msgData;
        }
        else
        {
            $msg = "Password doesn't match";
            $type = "danger";
            $msgData = array(
                'msg' => $msg,
                'type' => $type
            );
            return $msgData;
        }
    }

}

需要更改的此登录详细信息表单实际上是单个视图本身内的4个不同选项卡中的单个选项卡。(完整页面代码在代码段中提供以供参考)。如果单击按钮后立即不通过闪存显示状态消息,这可能是一个问题吗?

此外,我已经完成了检查,就好像密码和确认密码输入匹配一样。但是,它似乎没有正确执行它。相反,我总是得到一个成功的信息。

对此方面的任何建议都表示赞赏。

完整视图代码的代码段

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Student Dashboard View</title>
<meta name="description" content="">
...
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="<?php echo base_url("assets/js/customScripts/taro/bootstrap-datepicker.js")?>"></script>
<script>window.jQuery || document.write('<script src="<?php echo base_url("assets/js/customScripts/taro/vendor/jquery-1.10.1.min.js")?>"><\/script>')</script>
<!--<script src="--><?php //echo base_url("assets/js/customScripts/taro/vendor/jquery.hashchange.min.js")?><!--"></script>-->
<script src="<?php echo base_url("assets/js/customScripts/taro/vendor/jquery.easytabs.min.js")?>"></script>

<script src="<?php echo base_url("assets/js/customScripts/taro/main.js")?>"></script>

<style>
    .entry:not(:first-of-type)
    {
        margin-top: 10px;
    }

    .glyphicon
    {
        font-size: 21px;
    }
     .centered {
         position: fixed;
         left: 50%;
         transform: translate(-50%, -50%);
     }
</style>


<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script>window.html5 || document.write('<script src="<?php echo base_url("assets/js/customScripts/taro/vendor/html5shiv.js")?>"><\/script>')</script>
<![endif]-->

<!--[if lt IE 7]>
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
<![endif]-->
<div class="main-container">
    <div class="">
        <!-- Header Start -->
        <header id="header">
            <div id="logo">
                <h2>
                    <?php foreach($details as $detail){?>
                        <?php echo $detail->name;?>
                    <?php }?>
                </h2>
            </div>
        </header>
        <!-- Header End -->
        <!-- Main Tab Container -->
        <div id="tab-container" class="tab-container">
            <!-- Tab List -->
            <ul class='etabs'>
                <li class='tab' id="tab-about">
                    <a href="#about"><i class="icon-user"></i><span> User Details</span></a>
                </li>
                <li class='tab'>
                    <a href="#resume"><i class="icon-file-text"></i><span> Edit Resume</span></a>
                </li>
                <li class='tab'>
                    <a href="#login"><i class="fa fa-cloud"></i><span> Login Credentials</span></a>
                </li>
                <li class='tab'>
                    <a href="#contact"><i class="icon-envelope"></i><span> Edit Contact Details</span></a>
                </li>
            </ul>
            <!-- End Tab List -->
            <div id="tab-data-wrap">
                <!-- About Tab Data -->
                <div id="about">
                    <section class="clearfix">
                        <form class="form-horizontal" method ="post" role="form" enctype="multipart/form-data">
                            <?php echo form_open('studentDashboardController/saveUserDetails'); ?>
                            <?php echo $this->session->flashdata('msg'); ?>
                            <fieldset>

                                <!-- Form Name -->
                                <legend>User Details</legend>
                                ...
                                <!-- Button -->
                                <div class="form-group">
                                    <label class="col-md-4 control-label" for="submit"></label>
                                    <div class="col-md-4">
                                        <a href="<?php echo base_url("/index.php/studentDashboardController/saveUserDetails"); ?>" >
                                            <button id="submit" name="submit" type="submit" class="btn btn-primary">Save Changes</button>
                                        </a>
                                    </div>
                                </div>

                            </fieldset>
                        </form>

                    </section>
                </div>
                <!-- End About Tab Data -->

                <!-- Resume Tab Data -->
                <div id="resume">
                    <section class="clearfix">
<!--                        <form class="form-horizontal" method = "post" action="--><?php //echo site_url('studentDashboardController/saveUserResumeDetails');?><!--">-->
                        <div class="g2">
                            <h3>
                                Work Experience
                            </h3>
                            ...
                    <div class="form-group">
                        <label class="col-md-4 control-label" for="submit"></label>
                        <div class="col-md-4">
                            <a href="<?php echo base_url() ?>index.php/studentDashboardController/saveUserResumeDetails">
                                <button id="submit" name="submit" type="submit" class="btn btn-primary">Save Changes</button>
                            </a>
                        </div>
                    </div>
                </div>
                <!-- End Resume Tab Data -->

                <!-- Login Tab Data -->
                <div id="login">
                    <section class="clearfix">
                        <form class="form-horizontal" method = "post" action="<?php echo site_url('studentDashboardController/saveUserLoginDetails');?>">
                        <fieldset>

                        <div style="width:1363px;margin-left:10px">
                            <?php echo $this->session->flashdata('msgLogin'); ?>
                        </div>
                        <!-- Form Name -->
                        <legend>Edit Login Credentials</legend>

                        <!-- Text input-->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="username">Username</label>
                          <div class="col-md-6">
                          <input id="username" name="username" type="text" placeholder="current" class="form-control input-md">
                          <span class="help-block">Enter a new Username (Leave Blank to only change the Password)</span>
                          </div>
                        </div>

                        <!-- Password input-->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="password">Password</label>
                          <div class="col-md-6">
                            <input id="password" name="password" type="password" placeholder="New password" class="form-control input-md">

                          </div>
                        </div>

                        <!-- Password input-->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for="confirmPassword">Confirm Password</label>
                          <div class="col-md-6">
                            <input id="confirmPassword" name="confirmPassword" type="password" placeholder="Retype password" class="form-control input-md" required="">

                          </div>
                        </div>

                        <!-- Button -->
                        <div class="form-group">
                          <label class="col-md-4 control-label" for=""></label>
                          <div class="col-md-4">
                              <a href="<?php echo base_url() ?>index.php/studentDashboardController/saveUserLoginDetails">
                                <button id="submit" name="submit" type="submit" class="btn btn-primary">Save Changes</button>
                              </a>
                          </div>
                        </div>

                        </fieldset>
                        </form>

                    </section>
                </div>
                <!-- End LoginData -->

                <!-- Contact Tab Data -->
                <div id="contact">
                    ...

0 个答案:

没有答案