0

I am new to cakephp. Thanks all in advance. I am using Cakephp2.8.5 version. I have a form with input fields and a submit button. When I am submitting a form, the input fields should pass through the array and should store in a variable called $totalData and I want to store the array variable $totalData in a session variable in cakephp. I have written code in the Userscontroller's cartData function. Please help me find out how to declare an array and store it in a session variable in cakephp. My index.ctp page:

    <form  method="post" action="<?php echo $this->webroot ?>users/cartData?>""> 
   <table><thead>
          <th>Exam Name</th>
          <th>Venue Name</th>
          <th>Date of Exam</th>

          <th>Price / Course</th>  
          <th>Number of Exams</th>            
          <th>Add to Cart</th> 
         </thead>
         <tbody>
          <tr>      
             <td>
                <select name="course">   
                  <option value="">--Select--</option>            
                  <option value="ITIL Foundation">ITIL Foundation</option>
                  <option value="PMP">PMP</option>
                  <option value="Prince 2 Foundation">Prince 2 Foundation</option>
                </select>           
             </td>    
              <td><input type="text" name="venue" id="venue"></td>        
              <td><input type="text" name="Userdate" id="Userdate" ></td>   

              <td><input type="text" name="txtprice" id="Userdate" ></td>
              <td>
                <select name="num_exams">   
                  <option value="">--Select--</option>            
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                </select>           
             </td>
             <td><input type="submit" name="btnSubmit" value="Submit"></td>

         </tr></tbody>

      </table>

My `UsersController.php` page :

<?php
App::uses('CakeEmail', 'Network/Email');
class UsersController extends AppController
{   

  public function cartData()
  {

    if($this->request->is('post')|| $this->request->is('put'))
    { 
       $totalData = array(

        'exam' => $this->Form->input('course'),
        'venue' => $this->Form->input('venue'),
        'date' => $this->Form->input('Userdate'),
        'price' => $this->Form->input('txtprice'),
        'orders' => $this->Form->input('num_exams')

    );

// I have a confusion how to store array values in session variable

     $_SESSION['total_data'][] = $totalData; 



    }


  }
}

4 Answers 4

1

Always make the best effort to use the framework in all that's possible. Some very specific cases are a problem to use the Helpers on, but otherwise, always try and use them.

<?php
$courseOptions = array(
    'ITIL Foundation' => 'ITIL Foundation', 
    'PMP' => 'PMP',
    'Prince 2 Foundation' => 'Prince 2 Foundation'
); 
$examOptions = array(1,2,3); 
?>
<?=$this->Form->create('User', array('action' => 'cartData'))?>
<table>
    <thead>
        <tr>
            <th>Exam Name</th>
            <th>Venue Name</th>
            <th>Date of Exam</th>
            <th>Price / Course</th>  
            <th>Number of Exams</th>            
            <th>Add to Cart</th> 
        </tr>
    </thead>
    <tbody>
        <tr>      
            <td>
                <?=$this->Form->input('course', array('options' => $courseOptions)?>
            </td>
            <td><?=$this->Form->input('venue')?></td>
            <td><?=$this->Form->input('userdate')?></td>
            <td><?=$this->Form->input('txtprice')?></td>
            <td><?=$this->Form->input('num_exams', array('options' => $examOptions)); ?></td>
            <td><?=$this->Form->submit('Submit')?></td>
        </tr>
    </tbody>
</table>
<?=$this->Form->end()?>

All Form input helpers create an array based on the created form. In this case it generates a data[User][course] for name on each, and UserCourse for id (using first input as example).

On the controller side, you access your the data array on $this->request->data which will have that very structure (i.e. $this->request->data['User']['course'] to access the first input value).

Sign up to request clarification or add additional context in comments.

Comments

0

Use the Session component and write your array to the session using:

$this->Session->write('cart', $totalData);

The Session component is probably already included in your AppController.php, if not add it there (or in your UsersController.php if you will only use it there):

    var $components = array('Session');

(add 'Session' to any other components already listed)

4 Comments

I have to pass these session array values in foreach loop to display in view page.How can i do this?
Why means? I want to display the input fields data in a row below in a seperate table whenever I am submitting the form
how to pass this session variable in foreach to pass in the view page. I used $this->set('cartDatas',$totalData); When i was accessing in the view page using print_r($cartDatas); its showing that undefined variable cartDatas
I used $this->set('cartDatas',$totalData); in the controller
0
$totalData = array(

    'exam' => $this->Form->input('course'),
    'venue' => $this->Form->input('venue'),
    'date' => $this->Form->input('Userdate'),
    'price' => $this->Form->input('txtprice'),
    'orders' => $this->Form->input('num_exams')

);

Is this part of your code working? Can you show the debug of $totalData please?

6 Comments

Iam not sure . I used var_dump(totalData); die(); but its not working. Actually I want to display the input fields data in a row below in a seperate table whenever I am submitting the form like adding to cart
I think you should use '$totalData = array( 'exam' => $this->Form->input('course'), 'venue' => $this->Form->input('venue'), 'date' => $this->Form->input('Userdate'), 'price' => $this->Form->input('txtprice'), 'orders' => $this->Form->input('num_exams') );'
I think you should use :'
I think you should use : '$totalData = array( 'exam' => $this->request->data['course'], 'venue' => $this->request->data['venue'], 'date' => $this->request->data['Userdate'], 'price' => $this->request->data['txtprice'], 'orders' => $this->request->data['num_exams'] );' then debug the array.
How to debug this array.Actually i used var_dump($totalData); die() but no result
|
0

$this->Form->input() is a method of the FormHelper, it is intended to be used in the view not controller in order to generate form inputs alongside $this->Form->create() and $this->Form->end().

In your controller form inputs are accessed using $this->request->data['Model']['fieldname']

You can do something like this in your view

<?php 
$courseOptions = array(
    'ITIL Foundation' => 'ITIL Foundation', 
    'PMP' => 'PMP',
    'Prince 2 Foundation' => 'Prince 2 Foundation'
); 

$examOptions = array('1' => '1', '2' => '2', '3' => '3'); 
echo $this->Form->create('User', array('action' => 'cartData')); 
?>
<table>
    <thead>
        <tr>
            <th>Exam Name</th>
            <th>Venue Name</th>
            <th>Date of Exam</th>
            <th>Price / Course</th>  
            <th>Number of Exams</th>            
            <th>Add to Cart</th> 
        </tr>
    </thead>
    <tbody>
        <tr>      
            <td>
                <?php echo $this->Form->input('course', array('options' => $courseOptions)); ?>
            </td>
            <td><?php echo $this->Form->input('venue'); ?></td>
            <td><?php echo $this->Form->input('userdate'); ?></td>
            <td><?php echo $this->Form->input('txtprice'); ?></td>
            <td><?php echo $this->Form->input('num_exams', array('options' => $examOptions)); ?></td>
            <td><?php echo $this->Form->submit('Submit'); ?></td>
        </tr>
    </tbody>
</table>
<?php echo $this->Form->end(); ?>

You can then do something like this in your controller

<?php
App::uses('CakeEmail', 'Network/Email');
class UsersController extends AppController
{   
    public function cartData()
    {
        if($this->request->is('post')|| $this->request->is('put'))
        { 
            $totalData = array(
                'exam' => $this->request->data['User']['course'],
                'venue' => $this->request->data['User']['venue'],
                'date' => $this->request->data['User']['userdate'],
                'price' => $this->request->data['User']['txtprice'],
                'orders' => $this->request->data['User']['num_exams']
            );

            $this->Session->write('total_data', $totalData);

            // Alternatively you can do something like this
            $this->Session->write('total_data', $this->request->data['User']);
        }
    }
}

You may need to enable the Session component, I strongly advise reading the blog tutorial in the cookbook to get a grasp of these common components / helpers and how post data is accessed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.