I’ve created a page that requires a session variable which will be inserted into a form. The problem I’m having is my Session->read(); is putting the variable into the right field in my form.
here is the controller where the session variable(last) is created
function add($last=null){
$accounttemplates=$this->Template->find('list', array(
'fields'=>array('name'),
'conditions' => array(
'account_id' => $this->Auth->user('account_id'))));
//retrieve Account Id of current User
$accountid=$this->Auth->user('account_id');
$templatefields=$this->Field->find('all', array(
'conditions' => array(
'Field.template_id' => "10002")));
$this->set('accountid', $accountid);
$this->set('templatefields', $templatefields);
$this->set('accounttemplates', $accounttemplates);
if($this->request->is('post'))
{
$this->Field->create();
if ($this->Field->save($this->request->data))
{
if($this->request->data['submit'] == "type_1")
{
$last=$this->Session->write('last', $this->data['Field']['template_id']);
$this->Session->setFlash('The field has been saved');
$this->redirect( array('controller' => 'fields','action' => 'add_again',$last));
}
if($this->request->data['submit'] == "type_2")
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'templates','action' => 'index'));
}
}
else
{
$this->Session->setFlash('The field could not be saved. Please, try again.');
}
}
$this->set('last', $last);
}
in this function is where the session variable is read.
function add_again($last=null){
$accounttemplates=$this->Template->find('list', array(
'fields'=>array('name'),
'conditions' => array(
'account_id' => $this->Auth->user('account_id'))));
//retrieve Account Id of current User
//$accountid=$this->Auth->user('account_id');
$templatefields=$this->Field->find('all', array(
'conditions' => array(
'Field.template_id' => "10002")));
//$this->set('accountid', $accountid);
$this->set('templatefields', $templatefields);
$last=$this->Session->read('last');
$this->set('accounttemplates', $accounttemplates);
if($this->request->is('post'))
{
$this->Field->create();
if ($this->Field->save($this->request->data))
{
if($this->request->data['submit'] == "type_1")
{
$this->Session->setFlash('The field has been saved');
$this->redirect( array('controller' => 'fields','action' => 'add_again'));
}
if($this->request->data['submit'] == "type_2")
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'templates','action' => 'index'));
}
}
else
{
$this->Session->setFlash('The field could not be saved. Please, try again.');
}
}
}
here is the form im trying to input the variable $last into
<?php echo $this->Form->create('Field', array('action'=>'add'));?>
<td><?php echo $this->Form->input('name', array('label'=>false)); ?></td>
<td><?php echo $this->Form->input('description', array('label'=>false)); ?></td>
<td><?php echo $this->Form->input('default_value', array('label'=>false)); ?></td>
<td><?php echo $this->Form->input('template_id', array('value' => $last, 'type'=>'text'));?></td>
<?php echo $this->Form->hidden('active',array('default'=>'1')); ?>
in the first function is where the session variable last is created, im trying to put that variable in the view into the template_id.
You didn’t set the variable
$lastinto youradd_again()method. Just write the following line below where you are reading the value from session.