Why won't my flash message show? (Cake PHP 2.0) - php

In AppController:
public $helpers=array("Session","Html","Form");
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'MainPages', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'MainPages', 'action' => 'front')
)
);
In MainPagesController:
public function front()
{
$this->Session->setFlash('Your stuff has been saved.');
debug($this->Session->read('Message'));
//etc...
In default layout (default.ctp)
<div id="content">
<?php echo "Flash:" ?>
<?php echo $this->Session->flash(); ?>
The correct flash message shows in the debug but not on the view. What am I missing? PS It's not because of space after the ?>.
Edit: I have discovered that CakePHP is calling the session helper before the session component for some reason. Still trying to figure out how to fix it.

Simple way to create flash messages is to create their ctp file in app/view/element dir

Try
<div id="content">
<?php echo "Flash:" ?>
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Session->flash(); ?>
You need to define flash('auth') in your view to see authentication session flash messages.

My setflash works in this way..
Keep this in the App controller
function setFlash($message, $type = 'blue') {
//what ever the color you need..
$arr = array('red' => 'red', 'green' => 'green', 'yellow' => 'yellow', 'gray' => 'gray', 'blue' => 'blue');
$this->Session->setFlash($message, 'default', compact('class'), $arr[$type]);
}
and then call it from the controller action where you need to make the flash message as below..
$this -> setFlash("This is flash message","red");
now you need to make the flash in the layout(if you are using) or in your ctp file..
<?php echo $this->Session->flash() ?>
<?php echo $this->Session->flash('red') ?>

I think it is because you have an error here:
<?php echo "Flash:" ?>
You are missing the semi-colon
<?php echo "Flash:"; ?>

Related

Codeigniter: How to use form_open() in multiple view files

I am trying to get users to fill in much information so I need more than one form and page. I made a main_view.php which has a side bar on the left with links to sub1.php, sub2.php, sub3.php. On the right half of main_view.php, it displays the sub pages with corresponding forms. Part of the main_view.php looks like this:
<?php $view_path = "../../application/views/"?>
<span id="theFormChanger" >
<?php
?>
</span>
var currentPage = 0;
var subviews = ['sub1.php', 'sub2.php','sub3.php'];
$('#sub1').click(function(){
currentPage = 1;
$('#theFormChanger').load(viewpath + subviews[currentPage]);
});
Part of the code of sub view pages:
<?php echo form_open('v_controller'); ?>
<?php echo form_input(array( 'type' => 'text', 'id' => 'demail', 'name' =>'demail')); ?>
<?php echo form_input(array( 'type' => 'text', 'id' => 'dname', 'name' => 'dname')); ?>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?>
For ../application/controllers/,there is a v_controller.php:
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->helper('form');
$this->load->view('sub1');
$data = array(
'User_Name' => $this->input->post('dname'),
'User_Email' => $this->input->post('demail'));
}?>
Every time when I go to localhost:8000/main/main_view, the left part is fine but the right part says "Fatal error: Call to undefined function form_open() in main_view.php"
I searched around but couldn't find answers. I made sure everything is loaded in autoload.php.
Is this a routing problem? I can't directly go to view files? Please help me. Thank you!
You can load views on to a view file like so
application > views > default.php
<?php $this->load->view('template/common/header');?>
<?php $this->load->view('template/common/navbar');?>
<?php $this->load->view('template/' . $page);?>
<?php $this->load->view('template/common/footer');?>
And then on controller
<?php
class Example extends CI_Controller {
public function index() {
$data['page'] = 'common/example';
$this->load->view('default', $data);
}
}
you need to create NameOfController/NameOfMethod in the form open method in your view page.
replace our code by this one , it will be work for you.
<?php echo form_open('v_controller/index'); ?>

Yii alert widget

In view I have widget:
if ($content):
echo Alert::widget([
'options' => [
'class' => 'alert-info',
],
'body' => $content,
]);
endif;
That widget I want render not always, for example after save and atc. Now Now I have placed that widget between if condition, maybe exists some more clear way to render widget only in some cases.
I think Flash-Messages is what you want:
For Example:
In the controller you can do something like that:
<?php
Yii::app()->user->setFlash('success', "Data saved!");
$this->redirect(array('thing/view', 'id' => 1));
And in the view:
<?php if(Yii::app()->user->hasFlash('success')):?>
<div class="info">
<?php echo Yii::app()->user->getFlash('success'); ?>
</div>
<?php endif; ?>
And of course you can combine it with the alert-widget or with a custom-widget.
See full documentation: http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/

Update two models of different databases with single view in yii

I have a view (_form.php) with fields (name,summary) submit button. If I click on submit button, it should update Name field of One model and Summary field of another model.Both this models are of different databases.
Can anyone help on this. I tried the following for this
In _form.php(Test)
<?php echo $form->labelEx($model, ‘name’); ?>
<?php echo $form->textField($model, ‘name’, array(‘size’ => 60, ‘maxlength’ => 250)); ?>
<?php echo $form->error($model, ‘name’); ?>
<?php echo $form->labelEx(Test1::model(), ‘summary’); ?>
<?php echo $form->textField(Test1::model(), ‘summary’, array(‘size’ => 60, ‘maxlength’ => 250)); ?>
<?php echo $form->error(Test1::model(), ‘summary’); ?>
<?php echo CHtml::submitButton($model->isNewRecord ? ‘Create’ : ‘Save’); ?>
In TestController.php
public function actionCreate() {
$model = new Test;
if (isset($_POST['Test'])) {
$model->attributes = $_POST['Test'];
if ($model->save()) {
$modeltest1 = new Test1;
$modeltest1->attributes = $_POST['Test1'];
$modeltest1->Id = $model->Id;
if ($modeltest1->save())
$this->redirect(array('view', 'Id' => $model->Id));
}
}
$this->render('create', array(
'model' => $model,
));
}
This code is not working. How can I make it work for different databases. I followed the below link for this.
http://www.yiiframework.com/wiki/291/update-two-models-with-one-view/
This code actually should work, but its bad.
I assume that you dont understand at all what is model and what its doing in Yii, also how to render and create forms.
I'll try to explain how it should be.
1st of all dont use Test::model() in views, unless you want to call some function from it(but try to avoid it). It can be done by passing it from controller:
public function actionCreate() {
$model_name = new Name;
$model_summary=new Summary;
//something here
$this->render('create', array(
'name' => $model_name,
'summary'=>$model_summary,
));
}
When you make render you passing variables to your view (name_in_view=>$variable)
2nd. In your view you can use your variables.
<?php echo $form->labelEx($name, ‘name’);
echo $form->textField($name, ‘name’, array(‘size’ => 60, ‘maxlength’ => 250));
echo $form->error($name, ‘name’);
echo $form->labelEx($summary, ‘summary’);
echo $form->textField($summary, ‘summary’, array(‘size’ => 60, ‘maxlength’ => 250)); ?>
echo $form->error($summary, ‘summary’); ?>
echo CHtml::submitButton($model->isNewRecord ? ‘Create’ : ‘Save’); ?>
3rd. You need to understand what is model. It's class that extends CActiveRecord in this case. Your code in controller should loo like:
public function actionCreate() {
$model_name = new Name;
$model_summary=new Summary;
if (isset($_POST['Name']))
$model_name->attributes=$_POST['Name'];
if (isset($_POST['Summary']))
$model_name->attributes=$_POST['Summary'];
if ($model_name->save()&&$model_summary->save())
$this->redirect(array('view', 'Id' => $model->Id));
$this->render('create', array(
'name' => $model_name,
'summary'=>$model_summary,
));
}
$model->attributes=$_POST[] here is mass assignment of attributes, so they must be safe in rules. You always can assign attributes with your hands (1 by 1), or form an array and push it from array.

How to pass values throuh link in Zend Framework?

I want to pass values through
<a href= <?php $this->baseUrl()/admin/registration/activate/> </a>
this is my code an i have to pass user Id to this url to activate user. what I have to use I wll get my user id <?php $this->userid; ?>
please help
my action is
public function activateAction()
{ // Administrator actvate user
$user_name = $this->getRequest()->getParam('user_name');
$reg = new clmsRegistrationModel();
$reg->setActive($user_name);
}
inside your action
$this->_request->getParam('prop_id', null);
Lets assume you have Controller Index and Action Customer, so you can build you link with an Zend Framework View helper (inside your view script):
echo $this->url(array(
'controller' => 'index',
'action' => 'customer',
'prop_id' => 5
));
in your Controller:
customerAction() {
$propId = $this->getRequest()->getParam('prop_id');
}
After your comment (add to your view script):
<?php $postUrl = $this->url(array(
'controller' => 'admin',
'action' => 'registration',
'activate' => $this->userid
)); ?>
Activate

CakePHP submitting a form to right action

I have this in add.ctp:
<!-- File: /app/views/posts/add.ctp -->
<h1>Add Post</h1>
<?php
echo $form->create('Post');
echo $form->input('title');
echo $form->input('body', array('rows' => '3'));
echo $form->end('Save Post');
?>
and this in my controller:
function add(){
if (!empty($this->data)) {
if($this->Post->save($this->data)){
$this->Session->setFlash('Your post has been saved');
$this->redirect(array('action' => 'index'));
}
}
}
My question is how does CakePHP know that when the user hits submit, to send "data" to the function "add" in the controller?
By default CakePHP will send the form to the same action that displayed it.
You can change it in the view as follows:
echo $form->create('Post', array('action' => 'whatever'));
or if you want to redirect to another controller as well you can use this
echo $form->create('Post', array('url' => '/controller_name/action_name'));
As per the updated syntax below will work (CakePHP 2.4.x):
echo $this->Form->create('RegistrationsInout', array('action' => 'startroom'));
For cakephp 3.x
$this->Form->create('Post', ['url' => ['action' => 'post']]);
See doc

Categories