Welcome Guest, Not a member yet? Register   Sign In
ajax call on success redirect page from controller loaded in place of form
#1

[eluser]axmed[/eluser]
Hello Everyone

i have this problem cant find a solution for it.this is how it goes, i have Form using Ajax i would call a controller and it will respond by sending back the form with the validation errors, but when no errors are found and a redirect is made by the controller the Ajax call loads the redirected page in the place of the form, leaving me with a page in a page
(header header footer footer).
so how can i handle Calling the redirect() function in an AJAX call to go to another page.

here is the code

jquery:
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) {
                  $("#center").html(data);                  
          }
      })

   return false;
  })

PHP Controller
Code:
function get_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');
}

side notes rest library for template, Autoform is to create a form.

Any advices?
Thanks


Messages In This Thread
ajax call on success redirect page from controller loaded in place of form - by El Forum - 02-06-2012, 06:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB