douxunnian0423 2012-02-05 02:56
浏览 67
已采纳

CodeIgniter ajax调用使用jquery调用重定向时打印整个页面我div

no title that fits this bug but this how it goes, i have form with a submit button when pressed jquery ajax calls the controller and the form validation is done if it fails the form is redrawn if it passes the page is redirected to the home page with flash message successes and thats where the bug happens it redraws the whole page in the content(header header footer footer). i hope it makes sense seeing is believing so here is the code

side notes: "autform" is a lib for creating forms "rest" is a lib for templates.

the jquery code:

$("form.user_form").live("submit",function() {
  $("#loader").removeClass('hidden');
  $.ajax({
      async :false,
      type: $(this).attr('method'),
      url: $(this).attr('action'),
      cache: false,
      data: $(this).serialize(),
      success: function(data) {
                $("#center").html(data);
                     $('div#notification').hide().slideDown('slow').delay(20000).slideUp('slow');
                }
  })

  return false;
 }); 

the controller

 function forgot_password()
{
        $this->form_validation->set_rules('login',lang('email_or_login'), 'trim|required|xss_clean');
        $this->autoform->add(array('name'=>'login', 'type'=>'text', 'label'=> lang('email_or_login')));
        $data['errors'] = array();
        if ($this->form_validation->run($this)) {                               // validation ok
            if (!is_null($data = $this->auth->forgot_password(
                    $this->form_validation->set_value('login')))) {
                    $this-> _show_message(lang('auth_message_new_password_sent'));
            } else {
                $data['message']=$this-> _message(lang('error_found'), false);  // fail
                $errors = $this->auth->get_error_message();
                foreach ($errors as $k => $v){
                     $this->autoform->set_error($k, lang($v));
                }
            }
        }
        $this->autoform->add(array('name'=>'forgot_button', 'type'=>'submit','value' =>lang('new_password')));
        $data['form']=  $this->autoform->generate('','class="user_form"');
        $this->set_page('forms/default', $data);
        if ( !$this->input->is_ajax_request()) { $this->rest->setPage(''); }
        else { echo  $this->rest->setPage_ajax('content');       }
    }
}

function _show_message($message, $state = true)
{
    if($state)
    {
        $data = '<div id="notification" class="success"><strong>'.$message.'</strong></div>';
    }else{
        $data = '<div id="notification" class="bug"><strong>'.$message.'</strong></div>';
    }
    $this->session->set_flashdata('note', $data);
    redirect(base_url(),'refresh');
}

i think it as if the redirect call is caught by ajax and instead of sending me the home page it loads the home page in the place of the form.

thanks for any help regards

  • 写回答

2条回答 默认 最新

  • dougui4325 2012-02-06 22:56
    关注

    OK found the problem and solution, it seemed you cant call a redirect in the middle of an Ajax call that is trying to return a chunk of HTML to a div, the result will be placing the redirected HTML in the div.

    The solution as suggested by PhilTem at http://codeigniter.com/forums/viewthread/210403/ is when you want to redirect and the call is made by Ajax then return a value with the redirect URI back to Ajax and let it redirect instead.

    For anyone interested in the code:

    The Jquery Ajax code:

    $("form.user_form").live("submit", function(event) {
      event.preventDefault();
      $("#loader").removeClass('hidden');
      $.ajax({
          type: $(this).attr('method'),
          url: $(this).attr('action'),
          cache: false,
          dataType:"html",
          data: $(this).serialize(),
          success: function(data) {
              var res = $(data).filter('span.redirect');
              if ($(res).html() != null) {
                  [removed].href=$(res).html();
                  return false;
              }
              $("#center").html(data);
          },
          error: function() {
    
          }
      })
      return false;
    });  
    

    The PHP Controller

    function _show_message($message, $state = true, $redirect = '')
    {
        if ($state)
        {
            $data = '<div id="notification" class="success"><strong>'.$message.'</strong></div>';
        } else {
            $data = '<div id="notification" class="bug"><strong>'.$message.'</strong></div>';
        }
    
        $this->session->set_flashdata('note', $data);
    
        if ( !$this->input->is_ajax_request())
        {
            redirect(base_url() . $redirect, 'location', 302);
        }
        else
        {
             echo '<span class="redirect">'.base_url().$redirect.'</span>';
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?