使用codeigniter中的变量重定向到其他控制器

时间:2015-02-09 11:03:51

标签: php codeigniter

请问您的帮助如何重定向变量。这是我的代码。

function new_content(){
    $this->output->set_status_header(200);
    $this->output->set_header('Content-Type: application/json');

    $subj_id = $this->input->post('subj_id');
    $mod_id = $this->input->post('mod_id');
    $lesson_id = $this->input->post('lesson_id');

    $sort_id = rand(100,999);

    $data = array(
        'subj_id'   => $subj_id,
        'mod_id'    => $mod_id,
        'lesson_id' => $lesson_id,
        'sort_id'   => $sort_id
        );
    $create = $this->mdl_lc->_insert($data);
    // $link = base_url().'lesson_content/edit/'.$subj_id.'/'.$mod_id.'/'.$lesson_id.'/'.$sort_id;
    // echo base_url().'lesson_content/edit/'.$subj_id.'/'.$mod_id.'/'.$lesson_id.'/'.$sort_id;
    redirect('lesson_content/edit/'.$subj_id.'/'.$mod_id.'/'.$lesson_id.'/'.$sort_id);
}

function edit(){
    $subj_id = $this->uri->segment(3);
    $mod_id = $this->uri->segment(4);
    $lesson_id = $this->uri->segment(5);
    $sort_id = $this->uri->segment(6);
    $data['subj_id'] = $subj_id;
    $data['mod_id'] = $mod_id;
    $data['lesson_id'] = $lesson_id;
    $data['sort_id'] = $sort_id;
    $data['title'] = 'Lesson Content';
    $this->load->view('lesson_content_display',$data);
}

请注意:

  1. lesson_content是一个控制器。
  2. 编辑是一种功能。
  3. 其余的是我试图包含重定向的变量。
  4. 当我做一些Inspect Element时,结果就在那里,但它不会重定向到我想要去的链接。

2 个答案:

答案 0 :(得分:0)

您可以将URI段作为参数直接传递给编辑功能。

喜欢这个;

public function edit($subject_id = '', $mod_id = '', $lesson_id = '', $sort_id = '')
{
    if ($subject_id == '' OR $mod_id = '' OR $lesson_id = '' OR $sort_id = '')
    {
      // IF anything is empty?
      show_error('Something went wrong...');
    }
    else
    {
        $data = array(
           'subj_id' => $subject_id,
           'mod_id' => $mod_id,
           'lesson_id' => $lesson_id,
           'sort_id' => $sort_id
        );

        $this->load->view('lesson_content_display', $data);
     }
}

希望这有帮助。

答案 1 :(得分:0)

在我的控制器功能

$link = base_url().'lesson_content/edit/'.$subj_id.'/'.$mod_id.'/'.$lesson_id.'/'.$sort_id;
    $jdata['redirect_url'] = $link;
    $jdata['message'] = 'You have successfully created new content!';
    $jdata['result']  = "SUCCESS";                  
     echo json_encode($jdata); 
     exit();

这是我视图中的一些脚本

<script>
    function create_new(){
        $.post('<?php echo site_url('lesson_content/new_content'); ?>',
             {
                subj_id        : $('#subj_id').val(),
                mod_id         : $('#mod_id').val(),
                lesson_id      : $('#lesson_id').val()
             },
             function(data){


                  if(data.result=='SUCCESS'){
                    // $('#lesson_created').val();

                    $('#lesson_created').html(data.message);
                      //$('#lesson_created').show();
                      $('#lesson_created').fadeTo( 1500, 5.0, function(){
                        $('#lesson_created').fadeOut(4000);
                            // setTimeout(function(){window.location.reload();}, 2500);
                            window.location.href = data.redirect_url
                      });

                  }

            },'json'
        );
    }
</script>

我只是添加这行代码来运行json结果:window.location.href = data.redirect_url