重定向到另一个页面ajax和codeigniter

时间:2012-10-21 12:06:27

标签: ajax codeigniter

任何人都可以帮助我在用户注册表单成功后为重定向页面提供简单的解决方案。

我的控制器代码:

public function signup()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('user_name','Name','trim|required|min_length[4]xss_clean');
$this->form_validation->set_rules('signupemail','Email','trim|required|valid_email');
$this->form_validation->set_rules('password','Password','trim|required|min_length[8]xss_clean');
$this->form_validation->set_rules('confmpassword','Password Confirmation','trim|required|matches[password]');
$this->form_validation->set_rules('contactno','Contact No','trim|required');

if ($this->form_validation->run()) {

       // $this->welcome_model->InsertStudents();
        echo json_encode("Thank You, Successfully Completed");
        //$this->welcome();
    } else {

        $val = array(
            'name' => form_error('name'),
            'email' => form_error('email'),
            'password' => form_error('password'),
            'confirmpassword' => form_error('confmpassword'),
            'contactno' => form_error('contactno')
        );

echo json_encode(validation_errors());
    }
}

Ajax代码:

$(document).ready(function(){
    $('#message').hide();
            $('form').submit(function(){
                //alert('ok');     

                $.ajax({
                    url:this.action,
                    type:this.method,
                    data:$(this).serialize(),
                    success:function(data){
                         var obj = $.parseJSON(data);

                        $('#message').show();
                        if(obj['name']!=null)
                        {   

                            $('#message').text("");
                            $('#message').append(obj1);
                            $('#message').html(obj['name']);
                            $('#message').append(obj['email']);
                            $('#message').append(obj['password']);
                            $('#message').append(obj['confirmpassword']);
                            $('#message').append(obj['contactno']);
                        }
                        else
                        {   

                            $('#message').text("");
                            $('#message').html(obj);


                        }

                    },
                    erro:function(){
                        alert("Please Try Again");
                    }                        
                });
                return false;
            });                        
        });

1 个答案:

答案 0 :(得分:4)

success: function()某处添加:

window.location.href = '/page_to_redirect_to';

试试这个:

<script type="text/javascript">
  $(document).ready(function()
  {
    $('#message').hide();
    $('form').submit(function()
    {
      $.ajax(
      {
        url:"<?php echo base_url();?>index.php/c_user/signup",
        type:"POST",
        data:$(this).serialize(),
        success:function(data)
        {
          var obj = $.parseJSON(data);

          $('#message').show();

          if(obj['name']!=null)
          {
            $('#message').text("");
            $('#message').append(obj1);
            $('#message').html(obj['name']);
            $('#message').append(obj['email']);
            $('#message').append(obj['password']);
            $('#message').append(obj['confirmpassword']);
            $('#message').append(obj['contactno']);
          }
          else
          {
            window.location.href = '/page_to_redirect_to';
          }
        },
        error:function()
        {
          alert("Please Try Again");
        }
      });
      return false;
    });
  });
</script>