Edvinas Liutvaitis
Edvinas Liutvaitis

Reputation: 575

connecting pages in codeigniter

i have a simple question i have a form where user enters his details and when the submit button is clicked whit his details submitted to database it will take the user to a different page i am using codeigniter and i am new to this is there an easy way to do this ? tnx for you help. here is my cmv:

controller

    <?php

class Info extends CI_Controller{

    function index(){

        $this->load->view('info_view');
    }
    // insert data
    function credentials()
    {   
     $data = array(
         'name' => $this->input->post('name'),
         'second_name' => $this->input->post('second_name'),
         'phone' => $this->input->post('phone'),
         'email' => $this->input->post('email'),  
         );


          $this->info_model->add_record($data);


    }



 }

?>

model

<?php

class Info_model extends CI_Model {

    function get_records()
          {
          $query = $this->db->get('credentials');

          return $query->result();   
          }


    function add_record($data)
          {
          $this->db->insert('credentials', $data);
          return;
       }


}

?>

view

<html>
    <head>
    </head> 
 <body>
   <?php echo form_open('info/credentials'); ?>
     <ul id="info">  
       <li>Name:<?php echo form_input('name')?></li>
       <li>Second Name: <?php echo form_input('second_name');?></li>
       <li>Phone: <?php echo form_input('phone');?></li>
       <li>Email: <?php echo form_input('email');?></li>
       <li><?php echo form_submit('submit', 'Start survay!!' );?></li>
     </ul>  

 <?php echo form_close();?>
  </body>
</html>

Upvotes: 0

Views: 99

Answers (3)

Rahul Chipad
Rahul Chipad

Reputation: 2401

You can also use refresh as a second parameter:

$this->info_model->add_record($data);
redirect('controllerName/methodName','refresh');

Upvotes: 0

jswetzen
jswetzen

Reputation: 698

You could use the redirect() function from the URL Helper to actually redirect the user
(http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html)

Like this:

$this->load->helper('url');
redirect('/some/other/page');

Note that this has to be called before any data is outputted to the browser.

Another way of doing it is to simply have two different views that you load depending on the context. Normally you want some form validation as well so you can use that to direct the user. I usually end up with something like this in my function, which is used both for posting the data, inserting it to the database and "redirecting":

$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required|trim|xss_clean');
/* More validation */

if ($this->form_validation->run() !== FALSE) {
    $data = array(
     'name' => $this->input->post('name'),
     'second_name' => $this->input->post('second_name'),
     'phone' => $this->input->post('phone'),
     'email' => $this->input->post('email'),  
     );
    $this->info_model->add_record($data);

    $this->load->view('some_other_view');
} else {
    $this->load->view('info_view');
}

Upvotes: 0

Mudshark
Mudshark

Reputation: 3253

If all you need is a simple redirect upon submission of the form:

$this->info_model->add_record($data);
redirect('controller/method');

Upvotes: 2

Related Questions