【问题标题】:how to get form input fields value in an array in cakephp如何在cakephp的数组中获取表单输入字段值
【发布时间】:2016-12-05 09:41:02
【问题描述】:

我是 cakephp 新手。提前谢谢大家。我正在使用Cakephp2.8.5 版本。我有一个带有输入字段和提交按钮的表单。当我提交表单时,输入字段应通过数组并应存储在名为$totalData 的变量中,我想将数组变量$totalData 存储在cakephp 的会话变量中。我在用户控制器的cartData 函数中编写了代码。请帮助我了解如何声明一个数组并将其存储在 cakephp 的会话变量中。 我的index.ctp 页面:

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



    }


  }
}

【问题讨论】:

    标签: cakephp cakephp-2.0 cakephp-2.x


    【解决方案1】:

    始终尽最大努力尽可能地使用该框架。一些非常具体的情况是使用 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()?>
    

    所有表单输入助手都会根据创建的表单创建一个数组。在这种情况下,它会为每个名称生成一个 data[User][course],并为 id 生成一个 UserCourse(以第一个输入为例)。

    在控制器端,您可以访问 $this-&gt;request-&gt;data 上的数据数组,该数组将具有该结构(即 $this-&gt;request-&gt;data['User']['course'] 访问第一个输入值)。

    【讨论】:

      【解决方案2】:

      使用 Session 组件并将您的数组写入会话:

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

      Session 组件可能已经包含在您的 AppController.php 中,如果没有将它添加到那里(或者在您的 UsersController.php 中,如果您只在其中使用它):

          var $components = array('Session');
      

      (将“会话”添加到已列出的任何其他组件)

      【讨论】:

      • 我必须在 foreach 循环中传递这些会话数组值才能显示在视图页面中。我该怎么做?
      • 为什么意味着?每当我提交表单时,我想在单独的表格中的下面一行中显示输入字段数据
      • 如何在foreach中传递这个session变量来传递视图页面。我用 $this->set('cartDatas',$totalData);当我使用 print_r($cartDatas); 在视图页面中访问时它显示未定义的变量 cartDatas
      • 我使用了 $this->set('cartDatas',$totalData);在控制器中
      【解决方案3】:
      $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 的调试信息?

      【讨论】:

      • 我不确定。我使用了 var_dump(totalData);死();但它不工作。实际上,每当我提交表单(例如添加到购物车)时,我都想在单独的表格中显示输入字段数据。
      • 我认为你应该使用 '$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 = 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'] );'然后调试数组。
      • 如何调试这个数组。其实我用过 var_dump($totalData); die() 但没有结果
      【解决方案4】:

      $this-&gt;Form-&gt;input() 是 FormHelper 的一种方法,它旨在用于视图而非控制器中,以便在 $this-&gt;Form-&gt;create()$this-&gt;Form-&gt;end() 旁边生成表单输入。

      在您的控制器表单中,使用$this-&gt;request-&gt;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 掌握这些常用组件/助手以及如何访问发布数据

      【讨论】:

        猜你喜欢
        • 2012-06-05
        • 2017-10-27
        • 1970-01-01
        • 2022-11-17
        • 1970-01-01
        • 2019-11-22
        • 2015-04-26
        • 2017-09-27
        相关资源
        最近更新 更多