views:

51

answers:

3

Hi, im having problems starting a codeigniter project, the problem is that when i do something in a controller and then i want a page to display the result, an example: i have a form to add a item to the database, i get all the data in the controller and save it to database and then i want (if all went well) to redirect to the main page with a success msg, i was doing this with

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

the problem is that the url keeps saying admin/addItem so everytime the page gets refreshed it adds another item, now i found the:

redirect('admin','refresh');

but this only helps me when i dont need to display any msg because this function dont allow to send a $data var. Any ideas? Probably this is really easy to fix but i cant find a way to handle the flow of the application the way i want, any help is apreciated. thanks ;)

+1  A: 

Could be easy to fix or it could be also difficult to fix, look at the documentation about uri routing http://codeigniter.com/user_guide/general/routing.html

In my test app i use this vars

$route['default_controller'] = "main";
$route['scaffolding_trigger'] = "";

main is a controller so if you visit your site it call at first the controller main

in your case i would use something like this

$route['admin/:num'] = "catalog/products"; 

This would redirect any access to admin/* to catalog/products

Be creative ;-)

streetparade
+2  A: 

Flashdata is your friend, follow that link and about halfway through the page you'll find the docs for flashdata.

Basically if your form functions are successful you set flashdata with your success message, $this->session->set_flashdata('success', 'You successfully did something, yay!'); Now redirect to your admin page. redirect('admin','refresh'); on your admin page check for existing flashdata and echo it to the user

Dyllon
oh this looks like what i need, im gonna check it ;D
SinneR
A: 

Hi,

I think you should instead use this aproach:

instead of calling the admin view or redirect the header, you should send the post from the form direct to the admin function, inside the admin function if the $_POST variable is not empty means that you should save the data in database calling other function in the same controller or in one model example:($this->save_data_in_database();) and send the result of it to the view again. If the $_POST variable is empty you should call the admin view and send the data for message as NULL.

I send one example bellow:

View Admin: (Post Directly to the admin function in controller)

<html>
(...)
<body>

<?php echo $message_response; ?>

<form method="post" action="<domain>/index.php/admin" />
   <imput (...)     
</form>

</html>

Controller:

function admin() 
{
   if (isset($_POST['fieldname']))
        $data['message_response']=$this->save_data_in_database($params...);
   else
        $data['message_response']=NULL;       

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


function save_data_in_database($params...)
{

(save data to database etc...)

return (...message_to_display...);

}

Best Regards,
Pedro

Pedro
this looks good also, right now im trying the flashdata thing if that didnt work like i want it im gonna try this, ty ;)
SinneR