Jenz
Jenz

Reputation: 8369

Page redirection in Codeigniter

I am a newbie to Codeigniter. As part of studying it, I created a form which inserts data to the database. After insertion it is not redirecting properly to the form view page (formvalidation.php).

Form Action

    public function submitform()
    {
        $formdata['username']=$this->input->post('username');
        $formdata['address']=$this->input->post('address');
        $formdata['state']=$this->input->post('state');
        $formdata['country']=$this->input->post('country');
        $data['state']=$this->getstatearray();
        $data['country']=$this->getcountries();
        if($this->userprofile_model->setdata($formdata)=='success')
        {
            $this->load->view('formvalidation/formvalidation',$data);
        }
        else
        {
            $this->load->view('formvalidation/formvalidation',$data);
        }
    }

Form view page (formvalidation.php)

echo form_open('formvalidation/submitform'); 
echo form_input('username');
echo "<br>".form_textarea("address");
echo "<br>".form_dropdown('state',$state);
echo "<br>";
echo form_dropdown('country',$country);
echo "<br><br>".form_submit('submit','Submit');
echo form_close();

Model (userprofile_model.php)

class userprofile_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
    }

    public function setdata($data)
    {
        $this->db->insert('userprofile',$data);  
        if($this->db->affected_rows()>0)
        {
            echo "success";
        }
        else 
        {
            echo "fail";
        }
    }
}

Now the value is getting inserted into the database. But after insertion I want to redirect it to the url http://www.example.com/codeigniter/index.php/formvalidation/. But instead, it is now redirecting to http://www.example.com/codeigniter/index.php/formvalidation/submitform where submitform is the form action method.

How can I solve this problem? I tried with putting exit; after the page redirect function. But it won't work. Please help me to fix this.

Thanks in advance.

Upvotes: 0

Views: 2006

Answers (4)

Nooha Haris
Nooha Haris

Reputation: 41

After insertion give the below code

redirect('controller_name/function_name');

Upvotes: 0

novice
novice

Reputation: 157

I use redirect('yourControllerName', 'refresh').

Read more here: Redirect()

Upvotes: 0

Nouphal.M
Nouphal.M

Reputation: 6344

yes it will, because there is no redirect code. When the form is submitted your submitform function is executed and in that function you are loading the view. So if you want to redirect to something else you must use the redirect() function.

public function submitform()
    {
        $formdata['username']=$this->input->post('username');
        $formdata['address']=$this->input->post('address');
        $formdata['state']=$this->input->post('state');
        $formdata['country']=$this->input->post('country');
        $data['state']=$this->getstatearray();
        $data['country']=$this->getcountries();
        if($this->userprofile_model->setdata($formdata)=='success'){
                 redirect("Your URL");
        }
        else{
                redirect("Your URL");
        }
    }

Also you must properly sanitize the user input. You must validate your form inputs before passing it to model. You can find more info here

Upvotes: 3

Phoenix
Phoenix

Reputation: 1530

First Load the view page in controller index function.

function index()
{
$this->load->view('v_bank_master');
}

Then in model page, after insertion done. give the below code.

redirect('give your controller page name here','refresh');

Your page will now redirect to the view page after insertion.

Hope it will help you...

Upvotes: 1

Related Questions