在php

时间:2017-07-28 09:24:47

标签: javascript php jquery html

我正在使用此代码请求新用户在首次登录时更改密码。所以这是我的表单,首次登录后会在弹出窗口中打开:

<div class="modal" id="change-password-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="static" data-keyboard="false">
          <div class="modal-dialog modal-lg" role="document" style="top:20%">
            <div class="modal-content content-align">
              <div class="modal-header">
                <h3 class="modal-title text-center modal-title-col">Change your password</h3>
              </div>
              <div class="modal-body">
                <form id="change-password" class="ns-form half-width" method="post" action="/profile/changepassword">
                  <?php if(empty($_POST) || isset($exc)): ?>
                  <label>
                    <input class="input-box" type="password" name="curr_pwd" required="required" maxlength="64" placeholder="Current Password" required="required" />
                    <?php if(isset($exc) && $exc->getCode() == eInfo::INVALID_CREDENTIALS): ?>
                      <span class="error msg-block bottom">
                        <?php echo stripslashes($exc->getMessage()); ?>
                      </span>
                    <?php endif; ?>
                  </label>
                  <label>
                    <input class="input-box" type="password" name="new_pwd" required="required" maxlength="64" placeholder="New Password" />
                      <?php if(isset($exc) && $exc->getCode() == eInfo::INVALID_PASSWORD): ?>
                        <span class="error msg-block bottom">
                          <?php echo stripslashes($exc->getMessage()); ?>
                        </span>
                      <?php endif; ?>
                  </label>
                  <label>
                    <input class="input-box" type="password" name="retype_pwd"
                           required="required" maxlength="64" placeholder="Re-type Password" />
                  </label>
                  <center> <input type="submit" value="Change" class="change-button" id="change-button" /> </center>
                  <?php endif; ?>
                </form>
                <?php if(!empty($_POST) && !isset($exc) && isset($message)): ?>
                    <br/>
                    <strong><?php echo $message; ?></strong>
                    <br/>
                    <center><a href="/">Go home</a></center>
                <?php endif; ?>
              </div>
              </div>
            </div>
          </div>
        </div>

当我点击提交功能时,它需要我描述/更改密码,我已经写完了。我希望我的表单在弹出窗口中提交,不会重定向到其他页面。

控制器中的profile / changepassword函数

public function changepassword() {
        User::authenticate();
        $this->log->debug("Inside " . __METHOD__ . "()...");

        $this->template->title = 'ChangePassword - '.TITLE_SUFFIX;

        if(!empty($_POST)) {
            try {
                $allowed = array('curr_pwd', 'new_pwd', 'retype_pwd');
                if($allowed != array_keys($_POST)) {
                    loadException('InvalidFormSubmissionException');
                    throw new \InvalidFormSubmissionException();
                }

                User::validatePassword($_POST['new_pwd']);

                if($_POST['new_pwd'] !== $_POST['retype_pwd'])
                    throw new model\exceptions\user\InvalidPasswordException('Passwords do not match!');

                if($_POST['curr_pwd'] === $_POST['new_pwd'])
                    throw new model\exceptions\user\InvalidPasswordException('Current password and new password should not be same!');

                $userService = new model\UserService();
                if(!($user = $userService->verifyPasswordForUser($_SESSION['userId'], $_POST['curr_pwd']))) {
                    loadException('user/InvalidCredentialsException');
                    throw new model\exceptions\user\InvalidCredentialsException('Wrong password!');
                }
                /*var $user User */
                $user->setPassword(md5($_POST['new_pwd']));
                $user->getPwdUpdated(1);
                $_SESSION['pwd_updated']=1;
                $userService->update($user);
                $this->template->message = 'Your password has been changed.';
            } catch(model\exceptions\user\InvalidCredentialsException $e) {
                $this->log->debug($e->getMessage());
                $this->template->exc =$e;
            } catch (\InvalidFormSubmissionException $e) {
                $this->log->debug($e->getMessage());
                $this->template->exc = $e;
            } catch(model\exceptions\user\InvalidPasswordException $e) {
                $this->log->debug($e->getMessage());
                $this->template->exc = $e;
            } catch(\Exception $e) {
                $this->log->debug($e->getMessage());
                $this->template->exc = $e;
            }
        }
        $this->log->debug("Peak memory usage in " . __METHOD__ . " = " . (memory_get_peak_usage(TRUE) / 1024) . " KB");
        $this->template->setLayout('profile');
        $this->template->show();
    }

这是一个基本的小问题,但我不知道该怎么做。感谢!

1 个答案:

答案 0 :(得分:0)

将changepassword中的代码移动到您的表单所在的页面,以便您可以将表单发送到自己的页面,因此不需要重定向,或者您必须使用AJAX