繁体   English   中英

在表单提交中调用jQuery函数在php中

[英]Call jquery fuction on form submit in php

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

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

当我单击“提交”功能时,它会将我带到我编写的配置文件/更改密码中。 我希望我的表单仅在弹出窗口中提交,而不要重定向到其他页面。

控制器中的配置文件/更改密码功能

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM