0

I want to pass my current page url from views (user/index.php), to controllers (User.php) because I want to use the url parameter in controllers.

By the way my System has only one edit page (user/index.php), and this page will be use in two different page, the (views/home.php) and (views/faculty/detail.php). Each edit button in these two pages I made an href to direct any of these two pages to edit pages. And at the end of href, I put parameter eg: edit button for detail.php href="<?php echo base_url()?>index.php/user/index/<?php echo $user->user_no ?>?prevpage=index.php/faculty/detail/" and user/index.php will show a url once clicked the edit button http://[::1]/Test-bank/index.php/user/index/76?prevpage=index.php/faculty/detail/, And an edit button for detail.php href="<?php echo base_url()?>index.php/user/index/<?php echo $user->user_no ?>?prevpage=index.php/home/" and user/index.php will show a url clicked the edit button http://[::1]/Test-bank/index.php/user/index/1?prevpage=index.php/home/.

I want to pass these URL parameter, the ?prevpage=index.php/home/ and ?prevpage=index.php/faculty/detail/ to controllers (User.php) and make it a string in order for me to use it for redirect(base.url()); so I can redirect edit page (user/index.php) back to it previous page (views/home.php) or (views/faculty/detail.php).

(views/home.php)

    <div>
         <a href="<?php echo base_url()?>index.php/user/index/<?php echo $user->user_no ? >
          ?prevpage=index.php/home/" class="btn btn-primary">Edit Info</a>
    </div>

(views/faculty/detail.php)

    <div>
         <a href="<?php echo base_url()?>index.php/user/index/<?php echo $user->user_no ?>
          ?prevpage=index.php/faculty/detail/" class="btn btn-primary">Edit Info</a>
    </div>

controllers (User.php)

    <?php
       session_start();
       defined('BASEPATH') OR exit('No direct script access allowed');

       class User extends CI_Controller {
       function __construct()
     {
       // this is your constructor
        parent::__construct();
        $this->load->model('User_Model');
        $this->load->database();
        $this->load->helper('form');
        $this->load->helper('url');
     }

      public function index($user_no = ''){

     if(!isset($_SESSION['authorization'])  || ($_SESSION['authorization'] != '1' && $user_no != 
      $_SESSION['user_no'])){
        redirect(base_url().'index.php','refresh');
        exit();
    }
    $user = $this->User_Model->get_user($user_no);
    $data = array(
        'user'      =>  $user,
    );
    $this->load->view('user/index',$data);
    }

    public function save(){
    $user_no = $this->input->post('user_no') + 0;
    $is_user_full       = $this->User_Model->is_user_full();
    if($is_user_full){
        $_SESSION['result'] = 'ERROR: User List Full';
        redirect(base_url().'index.php/user/index/'.$user_no);
    }
    $is_username_exists = $this->User_Model->check_username($this->input->post('username'));
    if($is_username_exists && $user_no == 0){
        $_SESSION['result'] = 'ERROR: Username Exists';
        redirect(base_url().'index.php/user/index/'.$user_no);
    }
    $data = array(
        'first_name'        =>  $this->input->post('first_name'),
        'last_name'         =>  $this->input->post('last_name'),
        'username'          =>  $this->input->post('username'),
        'authorization'     =>  $this->input->post('authorization') + 0,
    );
    if( !empty($this->input->post('password')) && !empty($this->input->post('cpassword')) ){
        $data['password']   =  $this->input->post('password');
    }
    if($user_no > 0){
        $result = $this->User_Model->update($data,$user_no);
        $redirect = base_url().'******here where I will put the parameter***';

    }
    else{
        $result = $this->User_Model->create($data,$user_no);
        $redirect = base_url().'index.php/faculty/';

    }
    $_SESSION['result'] = ($result) ? 'SUCCESS!' : 'ERROR!';
    redirect($redirect);

    }

I don't know what to do there. Hope you could help me.

1
  • If you are talking about prevpage then use $this->input->get('prevpage') Commented Mar 17, 2021 at 5:37

2 Answers 2

0

There are two ways to do it. either use get function to get the parameter you set like this.

$redirect = base_url().$this->input->get('prevpage');

or you can use $_SERVER['HTTP_REFERER'] like this to get referrer page url.

Sign up to request clarification or add additional context in comments.

Comments

0

Make a hidden input on your view :

<?php
    $actual_link                = "0$_SERVER[REQUEST_URI]";
?>

<input type="hidden" id="yourID" name="yourName" autocomplete='off' class="form-control" value="<?php echo $actual_link?>">

On controller add :

$data = array(
    ...
    'thelink'    =>    $this->input->post('yourName'),
    ...
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.