【问题标题】:Ajax in cakephp not rendering page as expectedcakephp 中的 Ajax 未按预期呈现页面
【发布时间】:2014-04-05 03:40:35
【问题描述】:

我是 ajax/javascript/jquery 的新手,正在尝试将页面呈现到包含我的表单的页面上。我正在渲染它,但是当它渲染时,它会转到我的表单页面,除了它实际加载页面,忽略布局并删除我的表单。

这是我的控制器代码:

<?php
class PlayersController extends AppController{
       public $components = array('RequestHandler');
       public function listall() {
          $this->set ( 'players', $this->Player->find ( 'all' ) );
       }

       public function index() {
          if (!empty($this->request->data)) {
               $this->Player->create();
                  if ($this->Player->save ( $this->request->data )){    
                       $this->set ( 'players', $this->Player->find ( 'all' ));
                       $this->render('listall', 'ajax');        
                   }
            }
      }
  }?>

这是我对表单的看法:

    <?php 
       echo $this->Form->create();
       echo $this->Form->input('name',array('id' => 'name')); 
       echo $this->Js->submit('Save', array(
        'before' => $this->Js->get('#inprogress')->effect('fadeIn'),
        'success' => $this->Js->get('#inprogress')->effect('fadeOut'),
        'update' => '#success'

        ));     
       echo $this->Form->submit('normal submit');
       echo $this->Form->end();
?>
        <div id="inprogress" style="display: none;background-color: 
                 lightblue">Saving in progress...</div>

这是我要渲染的视图:

      <p style="background-color: lightblue">Players</p>
       <table>
       <tr>
          <td>Name</td>
       </>tr
               <?php foreach ($players as $players): ?>
                 <tr>
                    <td><?php echo h($players['Player']['name']); ?>&nbsp;</td>
                 </tr> 
                     <?php endforeach; ?>
        </table>

这就是我在布局中引入 jquery 的内容:

    echo $this->Html->script('jquery-2.1.0.min');
    echo $this->fetch('meta');
    echo $this-Js->writeBuffer(array('cache' => TRUE));      

我一直在试图弄清楚如何在视图索引中实际呈现视图列表而不实际重新加载页面。现在很吃亏。

【问题讨论】:

    标签: javascript php jquery ajax cakephp


    【解决方案1】:

    很难理解您实际上在用您提供的信息做什么。那么试试这个-

    在你的控制器中

    <?php
    class PlayersController extends AppController{
           public $components = array('RequestHandler');
           public function listall() {
              $this->layout = 'ajax';
              $this->set ( 'players', $this->Player->find ( 'all' ) );
           }
    
           public function index() {
              if (!empty($this->request->data)) {
                   $this->Player->create();
                      if ($this->Player->save ( $this->request->data )){    
                            $this->Session->setFlash('Congrats');       
                       }
                }
          }
      }?>
    

    【讨论】:

    • 谢谢,它的作用是渲染索引视图,所以我最终得到了两个表单。我正在尝试做的事情:我的表单上现在有一个所有玩家的列表(listall)我想将一个玩家添加到列表中,然后在索引上呈现所有玩家的列表。所以当我使用js保存而不是提交时,基本上将listall视图渲染到索引视图。
    猜你喜欢
    • 1970-01-01
    • 2017-09-28
    • 2017-02-23
    • 2018-10-01
    • 2014-09-29
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多