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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失