2016-12-05 146 views
0

我是cakephp的新手。感謝所有提前。我正在使用Cakephp2.8.5版本。我有一個帶有輸入字段和提交按鈕的表單。當我提交表單時,輸入字段應該通過數組並存儲在名爲$totalData的變量中,並且我想將數組變量$totalData存儲在會話變量cakephp中。我在Userscontroller的cartData函數中編寫了代碼。請幫我瞭解如何聲明一個數組並將其存儲在cakephp中的會話變量中。 我index.ctp頁:如何在cakephp中獲取表單輸入字段值

<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; 



    } 


    } 
} 

回答

0

使用Session組件,並使用陣列寫入會話:

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

會話組件可能已經包含在您AppController.php,如果不加它那裏(或者在你的UsersController.php如果你只會用它存在):

var $components = array('Session'); 

(添加「會話」已經列出任何其他組件)

+0

我必須將這些會話數組值在foreach循環中顯示在視圖頁面中。如何才能做到這一點? –

+0

爲什麼?我想在下面的一行中顯示輸入字段數據,只要我提交表格 –

+0

'$ totalData = $ this-> Session-> read('cart');' –

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') 

); 

這段代碼是否工作正常?你能顯示$ totalData的調試嗎?

+0

Iam不確定。我用var_dump(totalData);死();但它不起作用。其實我想顯示輸入字段的數據在一個單獨的表下面的一行中,每當我提交表單就像添加到購物車 –

+0

我認爲你應該使用'$ totalData = array( 'exam'=> $ this->輸入('course'), 'venue'=> $ this-> Form-> input('venue'), 'date'=> $ this-> Form-> input('Userdate'), );''price'=> $ this-> Form-> input(' – Aarrbee

+0

我想你應該使用:' – Aarrbee

0

$this->Form->input()是FormHelper的一種方法,它旨在用於視圖而非控制器中,以便與$this->Form->create()$this->Form->end()一起生成表單輸入。

在您的控制器的形式輸入使用$this->request->data['Model']['fieldname']

你可以做這樣的事情在你看來

<?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(); ?> 

然後你可以做這樣的事情在你的控制器訪問

<?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']); 
     } 
    } 
} 

您可能需要啓用Session組件,我強烈建議在烹飪書中使用reading the blog tutorial以掌握這些常用組件mponents/helpers以及post數據訪問方式

0

始終盡最大努力在所有可能的情況下使用框架。一些非常特殊的情況是使用Helpers的問題,但除此之外,總是嘗試並使用它們。

<?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()?> 

所有的表單輸入助手都會根據創建的表單創建一個數組。在這種情況下,它爲每個名稱生成一個數據[User] [course],爲id生成一個UserCourse(以第一個輸入爲例)。

在控制器端,您訪問$this->request->data上的數據數組,該數組將具有該結構(即$this->request->data['User']['course']訪問第一個輸入值)。

相關問題