0

早晨,

在过去的 4 天里,我一直在绞尽脑汁和眼睛,我不明白为什么这不起作用。基本上它是一个简单的密码更改脚本,无需 ajax 即可完美运行。但是当我用 ajax 对其进行分层时,它似乎无法识别它,我不明白为什么。也许你们中的一位伟大的技术人员可以给我指路。

只是为了确认。似乎没有组织它是一个ajax请求,并且每次按下提交按钮时都会重新加载页面

所以形式

<form action="<?php echo site_url('site/new_password'); ?>" method="post" class="box validate" id="change_password_form">

    <div class="header">
        <h2>Change Password</h2>
    </div>

    <div class="content">

        <?php if (isset($no_match)) {?>
        <div class="alert error closeEverywhere">
            <span class="icon"></span>
            <strong>Error !</strong>&nbsp;&nbsp;Passwords don't match!
        </div>
        <?php } ?>
        <?php if (isset($changed)) {?>
        <div class="alert success closeEverywhere">
            <span class="icon"></span>
            <strong>Success !</strong>&nbsp;&nbsp;Password was successfully changed
        </div>
        <?php } ?>
        <div class="alert error closeEverywhere" id="alertMessage">
            <span class="icon"></span>
            <strong>Error !</strong>&nbsp;&nbsp;Password don't match!
        </div>
        <div class="alert success closeEverywhere" id="successMessage">
            <span class="icon"></span>
            <strong>Success !</strong>&nbsp;&nbsp;Password was successfully changed
        </div>
        <!-- The form -->
        <div class="form-box">

            <div class="row">
                <label for="change_pw">
                    <strong>Password</strong>
                    <small></small>
                </label>
                <div>
                    <input tabindex=1 type="password" class="required noerror" name="change_pw" id="change_pw" />
                    <?php echo form_error('change_pw','<label class="error" for="change_pw" generated="true">','</label>'); ?>
                </div>
            </div>

            <div class="row">
                <label for="change_pw_conf">
                    <strong>Again</strong>
                    <small>Password Confirmation</small>
                </label>
                <div>
                    <input tabindex=2 type="password" class="required noerror" name="change_pw_conf" id="change_pw_conf" />
                    <?php echo form_error('change_pw_conf','<label class="error" for="change_pw_conf" generated="true">','</label>'); ?>
                </div>
            </div>

        </div><!-- End of .form-box -->

    </div><!-- End of .content -->

    <div class="actions">
        <div class="left">
        </div>
        <div class="right">
            <input tabindex=3 type="submit" value="Change Password" name="change_btn" id="change_btn" />
        </div>
    </div><!-- End of .actions -->

</form>

和 AJAX 部分

$(document).ready(function () { 
$('#login_form').on('submit',function() {

    $.post(base_url+'site/login',$('#login_form').serialize(),function(data) {
        if(!data || data.status !=1 )
        {
            showError();
            return false;
        }
        setTimeout( "window.location.href='"+base_url+"site/new_password'", 1000 );

    },'json');  
    return false;
});

$('#change_password_form').on('submit',function() {

         $.post(base_url+'site/new_password',$('#change_password_form').serialize(),function(data) {
        if(!data || data.status !=1 )
        {
            showError();
            return false;
        }
        alert('Success');
        showSuccess();
        setTimeout( "window.location.href='"+base_url+"member_section'", 1000 );

    },'json');  
    return false;
});

function showError() {
    $('#alertMessage').slideDown(500);
}

function showSuccess() {
    $('#successMessage').slideDown(500);
}
 });

最后是控制器方法

  function new_password()
  {
      if ($this->input->is_ajax_request())  
      {
          $return_arr['status'] = 0;
          echo json_encode($return_arr); // return value 
          exit();
      }

      $this->load->view('site/new_password_view');  
  }
4

1 回答 1

1

我错过了你在那个javascript中定义base_url的地方吗?如果这不是我认为的整个问题,那么您的代码没有被发送到合法的 URL。base_url 是一个 CodeIgniter PHP 函数。因此,例如以下行

$.post(base_url+'site/new_password',$('#change_password_form').serialize(),function(data) {

应该读:

$.post("<?=base_url();?>site/new_password",$('#change_password_form').serialize(),function(data) {
于 2013-01-02T19:53:47.177 回答