CodeIgniter和ajax问题

时间:2013-01-02 11:08:21

标签: php ajax codeigniter

上午,

在过去的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');  
  }

1 个答案:

答案 0 :(得分: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) {