I'm having a hard time trying to figure out how to get the response from below code which is located in the Controller back in the View:
if ($this->request->isAjax()){
Configure::write ( 'debug', 0 );
$this->autoRender = 0;
$this->layout = 'ajax';
$results = "My Data";
echo json_encode($results);
}
//tried doing this and it doesn't work
$this->set('results',$results);
I just want be able to access the data that is in the $results variable back in my View.
If this is your entire method, it would be logical that nothing is shown in your view. Since you've disabled autoRendering with:
$this->autoRender = 0;
You need to manually render your view at some point, using:
$this->render('view');
Where view is the actual name of the view you want to render. Otherwise, you'll just get a blank page.
Also note that you're setting $results within your isAjax() check, but you set it to your view outside that check, so you will get an undefined variable error if a "regular" call (non-ajax) is made to your controller action.
Related
I have simple line, but it doesn't work.
$this->getView($input->get('my_wiew', 'Sites', 'CMD'), 'HTML');
//some code
parent::display();
If i simple go to the url index.php?option=com_my_component&view=sites i get my view, but by default it doesn't want to load.
$view = $this->getView('view_name', 'html'); //get the view
$view->assignRef('data', $data_from_model); // assign data from the model
$view->display(); // display the view
Read more
I just came across a bit of a puzzling situation. My controller action looks like this:
public function myaction($eventId = false) {
if(!$eventId) {
//list all events
$data = foo;
} else {
if(!$this->Event->findById($eventId) )
{
$this->Session->setFlash('myerror message', 'flash_frontend_message');
$this->redirect(array('controller' => 'events', 'action' => 'myaction'));
} else {
// display event information
$data = foo;
}
}
$this->set($data);
}
If I call the function with an $eventId that is not found in the database, it outputs an error message and the user gets redirected back to the list of all events. However lets say I then select an eventId which is valid, view the relevant information and then press the browser back button, the previous flash message gets out put again, even though the URL in this case does not contain the $eventId.
I suppose what happens is that the page gets loaded from the browser cache rather then reloaded. I have tried to avoid storing the view in the cache like this:
<!--nocache-->
<?php echo $this->Session->flash(); ?>
<?php echo $this->Session->flash('auth'); ?>
<!--/nocache-->
but still, the flash message gets displayed.
Any idea how to prevent this behavior?
Is there perhaps a way to clear the flash after it has been displayed?
you are talking about the right cache but trying to fix it with the wrong cache.
<!--nocache-->
is php/html no cache statements in the file itself. it has nothing to do with the browser cache.
what you need to be doing is setting the right headers.
there is a convinience method in the request object for this exact thing:
http://book.cakephp.org/2.0/en/controllers/request-response.html#interacting-with-browser-caching
I use it in all my normal frontend actions via AppController:
public function beforeRender() {
$this->response->disableCache();
...
}
Usually, I just set a $feedback var or array and then check for that to display in my views.
However, it occurred to me I should perhaps use flashdata instead.
The problem is sometimes - for say an edit record form, I may simply want to reload the form and display feedback - not redirect. when i use flashdata, it shows but then it shows on the next request as well.
What would be the best practice to use here?
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.
u use hidden field for that
I would use the validation errors from the Form validation class and load those directly to the view in its 2nd argument.
$this->form_validation->set_error_delimiters('<p>', '</p>');
$content_data = array();
if (!$this->form_validation->run()) {
$content_data['errors'] = validation_errors();
}
$this->load->view('output_page', $content_data);
Then check in your view whether $errors isset.
Controller:
$data['message'] = 'some message you want to see on the form';
$this->load->view('yourView', $data);
View:
if (isset ($message)) : echo $message; endif;
...
Ive been working with CI and I saw on the website of CI you can load a view as a variable part of the data you send to the "main" view, so, according the site (that says a lot of things, and many are not like they say ...ej pagination and others) i did something like this
$data['menu'] = $this->load->view('menu');
$this->load->view ('home',data);
the result of this is that I get an echo of the menu in the top of the site (before starts my body and all) and where should be its nothing, like if were printed before everything... I have no idea honestly of this problem, did anybody had the same problem before?
Two ways of doing this:
Load it in advance (like you're doing) and pass to the other view
<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
Load a view "from within" a view:
<?php
// put this in the controller
$this->load->view('home');
// put this in /application/views/home.php
$this->view('menu');
echo 'Other home content';
Create a helper function
function loadView($view,$data = null){
$CI = get_instance();
return $CI->load->view($view,$data);
}
Load the helper in the controller, then use the function in your view to load another one.
<?php
...
echo loadView('secondView',$data); // $data array
...
?>
Real basic CI question here, which I cant find anything on in the documentation. I think I may need some further configuration?? I have a function which loads a view and it works correctly, but when I send it parameters its doesn't load the view, any ideas??
Heres code with params (view does not load)
function grid($height,$width)
{
echo $height."x".$width;
$this->load->view("grid");
}
and here's without (view does load)
function grid()
{
//echo $height."x".$width;
$this->load->view("grid");
}
So Height and width is the only thing that echos in the first example, in the second the view is loaded.
Thanks ahead of time!
You are supposed to have your controller pass parameters to the view as an array:
function grid($height,$width)
{
$data = array();
$data['height'] = $height;
$data['width'] = $width;
$this->load->view("grid", $data);
}
Then your view can render them:
echo $height."x".$width;
This allows for a clean separation of concerns between the Controller and View objects.
For more information see the section Adding Dynamic Data to the View in the CI User Guide.