4

I am working on a project which deals with lots of forms. I searched and tried the search results, but none of them worked.

My code view code "uprofile.php" is as follows:

<form class="form-horizontal" method="post" action='<?php echo base_url() . "home/addnewagency" ?>'>
        <div class="form-group">
            <label for="company" class="col-sm-3 control-label">New Agency Name:</label>
            <div class="col-sm-4">
                <input type="text" class="form-control" id="newagency" name="newagency" placeholder="Plase Enter New Agency Name">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-3 col-sm-9">
                <button type="submit" class="btn btn-primary">Add</button>
            </div>
        </div>
    </form>

My Controller "home.php" code is as follows:

public function addnewagency() {
        $newagency = $this->input->post('newagency');
        $this->dis_model->newagency_model(strtoupper($newagency));
        $this->loadprofile();
    }
    public function loadprofile() {
        $data['inscompanyname'] = $this->dis_model->getcompany();
        $data['agentname'] = $this->dis_model->getagent();
        $data['agencyname'] = $this->dis_model->getagency();
        $data['clientname'] = $this->dis_model->getclient();
        $data['deptname'] = $this->dis_model->getdept();
        $this->load->view('uprofile', $data);
    }

when I submit the form, the control is passed to addnewagency() data gets inserted in the database and the URL is http://localhost/dis/home/addnewagency. After getting back to the calling function, it goes to loadprofile() and loads the view uprofile. But, the URL still remains the same. But, I want it as http://localhost/dis/home/login and also want to pass the data. Does anyone have any idea regarding how to achieve this?

All positive suggestion are welcomed...

Thanks in advance...

4
  • Whats in the same url? Commented Jun 22, 2016 at 10:04
  • Did you try redirect("home/login"); inside the function addnewagency() Commented Jun 22, 2016 at 10:06
  • Looks like you are doing this while you are in session. You can destroy the session after putting values and Redirect to login redirect("home/login");. just an Idea if that works Commented Jun 22, 2016 at 10:07
  • you have to redirect redirect(base_url() . 'home/loadprofile'); Commented Jun 22, 2016 at 10:15

4 Answers 4

4

Now you are calling $this->loadprofile() within addnewagency(). If you want to change URL, you need to use redirect() function of CodeIgniter.

Replace

$this->loadprofile()     

with below line of code and it will work for you.

redirect(base_url() . 'home/loadprofile');
Sign up to request clarification or add additional context in comments.

5 Comments

can i pass data using redirect() ?
in redirect() function you cant pass but you can set the data in session
But, there's a lot of data. Do you think, it would be feasible to store that much data is session?
You can store upto 4kb in CodeIgniter Sessions. One thing we can do it, Pass Id and fetch data from that ID on functioan load.
I don't think this is the best anwser since the OP asked to pass data as well
2

if you want to change the url use redirect(base_url() . 'home/loadprofile'); in your controller addnewagency(); as suggested by others and refer this question to know whether you can send data in redirect function or not, Sending data along with a redirect in CodeIgniter

Comments

2

I found a alternative option to get this done. You can't change URL string with $this->load->view function but you can redirect to target controller function using redirect method. Before you call redirect, set flash session data using $this->session->set_flashdata that will only be available for the next request, and is then automatically cleared. You can read it from official codeiginter documentation .

Example :

class Login extends CI_Controller {

    public function index()
    {
        if ($this->session->login_temp_data) {
            $data = $this->session->login_temp_data;
        } else {
            $data = array();
            $data['name'] = 'Guest' ;
        }
        $this->load->view('login', $data);
    }

    public function get_name()
    {
       $data = [];
       $data['name'] = 'John Doe';
       $this->session->set_flashdata('login_temp_data', $data);
       redirect("login");
    }

}

1 Comment

This is the best option in my opinion. And you can retrieve like so in the view isset($_SESSION['login_temp_data']) ? $login_temp_data = $_SESSION['login_temp_data'] : '';
0

You want to use the redirect() function

At the bottom of your addnewagency() method like so:

public function addnewagency() {
    $newagency = $this->input->post('newagency');
    $this->dis_model->newagency_model(strtoupper($newagency));
    redirect(base_url() . 'home/loadprofile');
}

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.